Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Date related query

Author  Topic 

kmakwana
Starting Member

1 Post

Posted - 2011-03-26 : 00:01:54
Hi All,

I have table with fields like date,product. i want o/p in below format
Note: day1= start date
P1=product one .....
can any one let me know how to do this?

period P1 p2 p3
day1 23 13 21
day2 1 4 10
..
day7
week2
....
week5
month2
....

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-03-26 : 17:01:15
Reading between the lines, I am suspecting that you don't know in advance what p1, p2, p3 might be, or how many of them there would be. In that case, you will need to use dynamic pivoting. See here: http://www.sqlteam.com/article/dynamic-cross-tabs-pivot-tables or http://weblogs.sqlteam.com/jeffs/archive/2005/05/02/4842.aspx

If you do know the products in advance you can use the PIVOT operator available in SQL 2005 and higher. Another alternative, if you know the products in advance, is discussed here: http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=158663
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-03-27 : 01:14:51
if its for reporting need, you can generate it very easily in any of reporting tools also.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -