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.
| Author |
Topic |
|
kwikwisi
Constraint Violating Yak Guru
283 Posts |
Posted - 2009-11-23 : 02:07:59
|
| Hi I like to retrieve rows as cols . Any idea is appreciated, thanks.Eg. Table1itemcode item1 apple2 orangeTable2itemcode quantity1 302 251 10Resultapple orange40 25 |
|
|
lionofdezert
Aged Yak Warrior
885 Posts |
Posted - 2009-11-23 : 02:36:01
|
| CREATE TABLE #Table1 (itemcode INT, item VARCHAR(25))INSERT INTO #Table1 SELECT 1,'apple'UNION ALLSELECT 2 ,'orange'GOCREATE TABLE #Table2 (itemcode INT, quantity INT)INSERT INTO #Table2 SELECT 1,30UNION ALLSELECT 2,25UNION ALLSELECT 1, 10GOSELECT [APPLE] ,[ORANGE] FROM (SELECT item,quantity FROM #Table1 INNER JOIN #Table2 ON #Table1.itemcode = #Table2.itemcode ) p PIVOT (SUM(quantity) FOR item IN ([APPLE],[ORANGE])) AS pvt |
 |
|
|
kwikwisi
Constraint Violating Yak Guru
283 Posts |
Posted - 2009-11-23 : 02:41:23
|
| Thanks for reply.Actually items are not fixed and they will be dynamic. |
 |
|
|
lionofdezert
Aged Yak Warrior
885 Posts |
Posted - 2009-11-23 : 02:44:23
|
| follow thishttp://www.sqlteam.com/article/dynamic-cross-tabs-pivot-tables |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
|
|
|