| Author |
Topic |
|
coagulance
Yak Posting Veteran
78 Posts |
Posted - 2008-12-25 : 02:47:04
|
| I have the below table which I would like to transposeCrate ItemNo ItemP001 1 T001P001 2 T101P001 3 T006P001 4 T090P002 1 T011P002 2 T111P002 3 T056P002 4 T080P003 1 T004P003 2 T103P003 3 T072P003 4 T091To as below:Crate Item1 Item2 Item3 Item4P001 T001 T101 T006 T090P002 T011 T111 T056 T080P003 T004 T103 T072 T091Can any one suggest some queries..Thanks in advance |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2008-12-25 : 03:31:12
|
| use pivot |
 |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2008-12-25 : 03:35:11
|
| try thisSELECT crate,[1] AS Item1,[2] AS Item2,[3] AS Item3,[4] AS Item4FROM (SELECT * FROM urtable) AS PPIVOT ( item FOR itemno IN ([1],[2],[3],[4]))AS PVT |
 |
|
|
Jai Krishna
Constraint Violating Yak Guru
333 Posts |
Posted - 2008-12-25 : 03:40:06
|
| SELECT crate,[1] AS Item1,[2] AS Item2,[3] AS Item3,[4] AS Item4FROM(SELECT * FROM yourtable) AS MPIVOT(MAX(Item) FOR ItemNo IN([1],[2],[3],[4])) AS PJai Krishna |
 |
|
|
Nageswar9
Aged Yak Warrior
600 Posts |
Posted - 2008-12-25 : 05:09:22
|
| Try This without using Pivot,,,select crate, max( case itemno when 1 then Item else NULL end ) as item1, max( case itemno when 2 then Item else NULL end ) as item2, max( case itemno when 3 then Item else NULL end ) as item3, max ( case itemno when 4 then Item else NULL end ) as item4from @tempgroup by crate |
 |
|
|
coagulance
Yak Posting Veteran
78 Posts |
Posted - 2008-12-25 : 08:49:47
|
| Thanks Nages that was of great help. |
 |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2008-12-25 : 10:26:50
|
quote: Originally posted by bklr try thisSELECT crate,[1] AS Item1,[2] AS Item2,[3] AS Item3,[4] AS Item4FROM (SELECT * FROM urtable) AS PPIVOT ( item FOR itemno IN ([1],[2],[3],[4]))AS PVT
This won't work. You need to use Jaikrishna Approach. |
 |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2008-12-25 : 23:00:30
|
quote: Originally posted by sodeep
quote: Originally posted by bklr try thisSELECT crate,[1] AS Item1,[2] AS Item2,[3] AS Item3,[4] AS Item4FROM (SELECT * FROM urtable) AS PPIVOT ( item FOR itemno IN ([1],[2],[3],[4]))AS PVT
This won't work. You need to use Jaikrishna Approach.
Thanks Sodeep.I had just missed Max(item value) |
 |
|
|
Nageswar9
Aged Yak Warrior
600 Posts |
Posted - 2008-12-26 : 00:00:44
|
WelCome |
 |
|
|
|