| Author |
Topic |
|
Quentin
Starting Member
12 Posts |
Posted - 2011-12-18 : 21:04:56
|
| T-SQLI need to group my results but I am having trouble understanding how to do it in this scenario.I have a table with the followingsampleid, name, valuedata is like thissampleid, name, value123, test1, 1234123, test2, 9876123, test3, 5564etcI would like to display the results as thissamplied, test1, test2, test3123, 1234, 9876, 5564I use the following codeSELECT SAMPLEID,CASE WHEN NAME = 'test1' THEN VALUE END AS TEST1CASE WHEN NAME = 'test2' THEN VALUE END AS TEST2CASE WHEN NAME = 'test3' THEN VALUE END AS TEST3FROM TESTTABLEI then want to add GROUP BY SAMPLEID but this is obviously incorrect as I need ALL the column names in the group by.I could use the MIN command but I need ALL the results.Is there a JOIN or UNION I can use to get the layout I need.Thanks |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-12-18 : 21:17:51
|
[code]SELECT SAMPLEID, MIN(CASE WHEN NAME = 'test1' THEN VALUE END) AS TEST1 MIN(CASE WHEN NAME = 'test2' THEN VALUE END) AS TEST2 MIN(CASE WHEN NAME = 'test3' THEN VALUE END) AS TEST3FROM TESTTABLEGROUP BY SAMPLEID[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-12-18 : 21:18:28
|
Also if you are using SQL 2005 / 2008, check out the PIVOT operator KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
Quentin
Starting Member
12 Posts |
Posted - 2011-12-18 : 21:57:14
|
| Thanks khtan, I had a look at PIVOT but it represents the same problem. To clarify, I have an additional column i forgot to mentionso the layout goes like thissampleid, duplicate, name, value123, PRIMARY, test1, 1234123, PRIMARY, test2, 9876123, PRIMARY, test3, 5564123, LR1, test1, 1234123, LR1, test2, 9876123, LR1, test3, 5564etcI therefore, cannot use min as I need all the results.If possible, the results should be like thisSampleid, Primary - test1,Primary - test2,Primary - test3, LR-test3, LR-test3, LR-test3123, 1234, 9876, 5564, 1234, 9876, 5564Hope that clarifies it a bit better |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-12-18 : 22:06:22
|
[code]SELECT SAMPLEID, MIN(CASE WHEN duplicate = 'PRIMARY' and NAME = 'test1' THEN VALUE END) AS Primary_TEST1, MIN(CASE WHEN duplicate = 'PRIMARY' and NAME = 'test2' THEN VALUE END) AS Primary_TEST2, MIN(CASE WHEN duplicate = 'PRIMARY' and NAME = 'test3' THEN VALUE END) AS Primary_TEST3, MIN(CASE WHEN duplicate = 'LR1' and NAME = 'test1' THEN VALUE END) AS LR_TEST1, . . .FROM TESTTABLEGROUP BY SAMPLEID[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-12-18 : 22:09:40
|
the PIVOT wayselect *from ( select sampleid, dup_name = duplicate + '-' + name, value from TESTTABLE ) d pivot ( min(value) for dup_name in ([PRIMARY-test1], [PRIMARY-test2], [PRIMARY-test3], [LR1-test1], [LR1-test2], [LR1-test3]) ) p KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
Quentin
Starting Member
12 Posts |
Posted - 2011-12-19 : 00:06:27
|
| Thanks khtan,That worked fine.Appreciate your assistance |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-12-19 : 00:12:20
|
welcome KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
|
|
|