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 |
Trinity99
Starting Member
2 Posts |
Posted - 2014-11-03 : 12:32:51
|
ALLI need to create a report out of our database.Simplified example:Table 1:LINK_ID Item123456 Item1123457 Item2123458 Item3123459 Item4Table 2Link_ID PROP_ID Property123456 1 Property_1123456 2 Property_2123456 3 Property_3123456 4 Property_4123457 1 Property_1123457 2 Property_2123457 3 Property_3123457 4 Property_4123458 1 Property_1123458 2 Property_2123458 3 Property_3123458 4 Property_4123459 1 Property_1123459 2 Property_2123459 3 Property_3123459 4 Property_4When I Join this 2 tables i get:LINK_ID Item PROP_ID Property123456 Item1 1 Property_1123456 Item1 2 Property_2123456 Item1 3 Property_3123456 Item1 4 Property_4123457 Item2 1 Property_1123457 Item2 2 Property_2123457 Item2 3 Property_3123457 Item2 4 Property_4123458 Item3 1 Property_1123458 Item3 2 Property_2123458 Item3 3 Property_3123458 Item3 4 Property_4123459 Item4 1 Property_1123459 Item4 2 Property_2123459 Item4 3 Property_3123459 Item4 4 Property_4But I need:LINK_ID Item PROP_ID1 PROP_ID2 PROP_ID3 PROP_ID4123456 Item1 Property_1 Property_2 Property_3 Property_4123457 Item2 Property_1 Property_2 Property_3 Property_4123458 Item3 Property_1 Property_2 Property_3 Property_4123459 Item4 Property_1 Property_2 Property_3 Property_4Thanks in advance for your help !!Ivo |
|
gbritton
Master Smack Fu Yak Hacker
2780 Posts |
Posted - 2014-11-03 : 12:41:22
|
Add a pivot clause:PIVOT Max(Property) for Prop_ID in ([1],[2],[3],[4]))p |
|
|
Trinity99
Starting Member
2 Posts |
Posted - 2014-11-03 : 12:58:44
|
Thank you very much but I got another answer in another forum which works for me:SELECT a.LINK_ID, Max(CASE WHEN Prop_ID = 1 THEN Property END) AS PROP_ID1, Max(CASE WHEN Prop_ID = 2 THEN Property END) AS PROP_ID2, Max(CASE WHEN Prop_ID = 3 THEN Property END) AS PROP_ID3, Max(CASE WHEN Prop_ID = 4 THEN Property END) AS PROP_ID4FROM table1 a INNER JOIN table2 b ON a.LINK_ID = b.LINK_IDGROUP BY a.LINK_ID |
|
|
gbritton
Master Smack Fu Yak Hacker
2780 Posts |
Posted - 2014-11-03 : 13:10:35
|
yes, that's another way of doing the same thing. |
|
|
|
|
|