Sunday, 15 August 2010

sql - Selecting data for columns based on a range of dates -


I have a week_date and net_sale table (as well as many other columns) for that table.

I am trying to write a statement which is <

style_number, net_sales

  for MAX (week_id) code, net_sales MAX (week_id) -1 for code   

.., max (week_ id) - n

so that the results are shown:

 ABCD, 125.00, 100.00 EFG, 75.00, 50.00  

What is the best way to reach it, especially when N is not big Maybe (52 weeks are looking back)?

I hope this makes sense! I'm using SQL Server 2008 R2 a lot thanks so much earlier!

You can use the PIVOT and dynamic SQL for your big weeks Deal with

  announcement @ cols nVARCHAR (max), @ sql nVARCHAR (max) SET @ cols = STUFF (SELECT DISTINCT), '+ Price ('.', 'NVARCHAR (MAX)'), 1, '') SET @sql = 'Select_style_Number by DIC for 1 xkm path (' ', type) from QUOTENAME (week_ID) sales , From '+ cols +' (select style_number, week_id, net_selets from sale) x pivot (more At the end of the week (net_sale) ('+ + cols +')) P ORDER BY style_number 'EXECUTE (@sql)   

Here is the demo.

No comments:

Post a Comment