Click here to Skip to main content
15,886,797 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
Hello Sir,

I have a table

SQL
   Date       Product       Qty
1-jan-2012     TR1           12
1-jan-2012     TR2           13
2-jan-2012     TR3           12
3-jan-2012     TR4           12
4-jan-2012     TR2           12
5-jan-2012     TR1           14


and i want to select data as


SQL
Product    1-jan-2012  2-jan-2012  3-jan-2012  4-jan-2012  5-jan-2012
 TR1          12            0          0            0          14
 TR2          13            0          0            12         0
 TR3          0             12         0            0          0
 TR4          0             0          12           0          0


Thanks for your valuable comments.
Posted

if you have few data to list you can use

SQL
SELECT
    Product    
    ,SUM(CASE WHEN [Date]='1-jan-2012' THEN Qty ELSE 0 END) 1jan2012
    ,SUM(CASE WHEN [Date]='2-jan-2012' THEN Qty ELSE 0 END) 2jan2012
    ,SUM(CASE WHEN [Date]='3-jan-2012' THEN Qty ELSE 0 END) 3jan2012
    ,SUM(CASE WHEN [Date]='4-jan-2012' THEN Qty ELSE 0 END) 4jan2012
    ,SUM(CASE WHEN [Date]='5-jan-2012' THEN Qty ELSE 0 END) 5jan2012

FROM
    Orders    
GROUP BY
    Product    


may be also pivot table could help Using PIVOT and UNPIVOT
 
Share this answer
 
v2
Comments
devbtl 18-Jan-12 2:59am    
Thanks katanakensei.
Use PIVOT - UNPIVOT

Using PIVOT and UNPIVOT[^]

Pivoting data in SQL Server[^](Alternate ways)
 
Share this answer
 
Comments
devbtl 18-Jan-12 2:59am    
Thanks thatraja.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900