| Author |
Topic |
|
Sathoris
Starting Member
5 Posts |
Posted - 2009-11-30 : 09:52:19
|
| Hi,I'm trying to change the format of a query result from:ID Name1 NULL1 Name11 Name22 Name23 Name34 NULL(Have a total of 5 options as Name1-5)IntoID Name1 Name2 Name3 Name4 Name51 1 1 0 0 02 0 1 0 0 03 0 0 1 0 04 0 0 0 0 0I'm using SQL 2000, I've saw this can be used simple with PIVOT which i don't have it in this SQL server version.Thanks, |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-11-30 : 09:57:45
|
| [code]SELECT ID,MAX(CASE WHEN Name='Name1' THEN 1 ELSE 0 END) AS Name1,MAX(CASE WHEN Name='Name2' THEN 1 ELSE 0 END) AS Name2,MAX(CASE WHEN Name='Name3' THEN 1 ELSE 0 END) AS Name3,...MAX(CASE WHEN Name='Name5' THEN 1 ELSE 0 END) AS Name5FROM TableGROUP BY ID[/code] |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
Sathoris
Starting Member
5 Posts |
Posted - 2009-11-30 : 10:04:07
|
| Thanks visakh16Your example works perfectly. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-11-30 : 10:05:18
|
| welcome |
 |
|
|
Sathoris
Starting Member
5 Posts |
Posted - 2009-11-30 : 10:28:12
|
| @visakh16Is there a way to update your query to work with unknown values ?As in Name1, Name2, Name3 can be anything from a list of 50+ but i want to show only the first 5 (A-Z order). |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-11-30 : 10:33:08
|
quote: Originally posted by Sathoris @visakh16Is there a way to update your query to work with unknown values ?As in Name1, Name2, Name3 can be anything from a list of 50+ but i want to show only the first 5 (A-Z order).
then follow the method given by Madhi |
 |
|
|
Sathoris
Starting Member
5 Posts |
Posted - 2009-11-30 : 11:45:18
|
| Thanks, I've tried Madhi's example and it worked perfectly but the results were not what I expected and I think I've expressed myself wrong.I'll try to give an example on how would be nice to have the results.Original Values:ID Name1 NULL1 Name11 Name21 Name52 Name42 Name103 Name34 NULL4 Name12Desired Results:ID 1stOption 2ndOption 3rdOption1 Name1 Name2 Name52 Name4 Name103 Name34 Name12The idea is to create an additional column only when there is a duplicated row with the same ID that is not null.I did this kind of reporting with C# code taken from the "Original Values" table, but in this case i need to do this with SQL only.If there is a way to get the the first value from each ID's into one select, then the 2nd values for each Id if it exists into a second select as in:This kind of results also works for me if it's easier to reproduce then the one above.Results #1:ID 1stOption1 Name12 Name43 Name34 Name12Result #2:ID 2ndOption1 Name22 Name10Result #3:ID 3rdOption1 Name5 |
 |
|
|
Sathoris
Starting Member
5 Posts |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-11-30 : 13:06:01
|
quote: Originally posted by Sathoris I've searched a bit this forum and i found an example to my 2nd request: http://sqlblogcasts.com/blogs/madhivanan/archive/2008/09/12/return-top-n-rows.aspxThanks problem solved.In case someone might know a solution to the first result it would be nice to have.
do you have any other unique valued column in your table? may be an id or date field? |
 |
|
|
|