August 19, 2010 at 2:23 pm
I have the following table:
ID, Description, Field1, Field2 ... Field9
Field1 to Field9 are all nullable fields.
I want a query to return as follows:
ID, Description, Field1
ID, Description, Field2
ID, Description, Field3
...
ID, Description, Field9
The row will be skipped if Fieldn is null.
Thanks.
Ronnie
August 19, 2010 at 2:29 pm
bonggoy cruz (8/19/2010)
I have the following table:ID, Description, Field1, Field2 ... Field9
Field1 to Field9 are all nullable fields.
I want a query to return as follows:
ID, Description, Field1
ID, Description, Field2
ID, Description, Field3
...
ID, Description, Field9
The row will be skipped if Fieldn is null.
Thanks.
Ronnie
My recommendation would be to use UNPIVOT. The documentation for the command is in Books Online.
--Jeff Moden
Change is inevitable... Change for the better is not.
August 19, 2010 at 2:47 pm
Thank you. I
August 20, 2010 at 9:42 am
You bet. Thanks for the feedback.
Also, since you're new to this forum, you might want to take a look at the first link in my signature line below. It could really make you're life easy on your next question. 🙂
--Jeff Moden
Change is inevitable... Change for the better is not.
August 24, 2010 at 12:51 pm
Try soemthing like this!!!
select ID, [Description], Field =
case when ID = 1 then Field1
when ID = 2 then Field2
when ID = 3 then Field3
when ID = 4 then Field4
when ID = 5 then Field5
when ID = 6 then Field6
when ID = 7 then Field7
when ID = 8 then Field8
when ID = 9 then Field9
else null
end
from #test
August 24, 2010 at 6:18 pm
jayraj0071 (8/24/2010)
Try soemthing like this!!!select ID, [Description], Field =
case when ID = 1 then Field1
when ID = 2 then Field2
when ID = 3 then Field3
when ID = 4 then Field4
when ID = 5 then Field5
when ID = 6 then Field6
when ID = 7 then Field7
when ID = 8 then Field8
when ID = 9 then Field9
else null
end
from #test
Try that against the OP's orginal data which looks like the following...
ID, Description, Field1, Field2 ... Field9
--Jeff Moden
Change is inevitable... Change for the better is not.
August 24, 2010 at 6:34 pm
somebody provide sample data of table!!!
August 24, 2010 at 7:49 pm
jayraj0071 (8/24/2010)
somebody provide sample data of table!!!
Heh... Why? OP is happy with the answer to lookup UNPIVOT. 😉
--Jeff Moden
Change is inevitable... Change for the better is not.
August 24, 2010 at 8:07 pm
I did use unpivot. I was actually aware and have used PIVOT before. I felt kind of stupid when UNPivot was suggested :D. Sometimes the most obvious answer is the one you overlook.
August 25, 2010 at 10:46 pm
bonggoy cruz (8/24/2010)
I did use unpivot. I was actually aware and have used PIVOT before. I felt kind of stupid when UNPivot was suggested :D. Sometimes the most obvious answer is the one you overlook.
Nah... definitely not stupid. We all forget things now and again. 🙂
Thanks for the feedback.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 10 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply