Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
I presently have the data as shown belowID P1N P1D P2N P2D P3N P3D <-- column names01 00A AAA 000 000 000 000 02 00B BBB 00A AAA 00C CCC03 00B BBB 00A AAA 000 000I want to display the above data as below ID PN0 PD0 <-- column names01 00A AAA02 00B BBB02 00A AAA02 00C CCC03 00B BBB03 00A AAAHow do i achieve this using SQL
webfred
Master Smack Fu Yak Hacker
8781 Posts
Posted - 2011-07-22 : 08:48:04
In the select list you can give an alias to each column name.selectP1N as PN0,P1D as PD0,...from ...No, you're never too old to Yak'n'Roll if you're too young to die.
bhasi75
Starting Member
2 Posts
Posted - 2011-07-22 : 08:58:50
webfred, please look at the data, i.e. the data for ID 02 which is in one row should be displayed in three rows as it is having data in (P1N & P1D) , (P2N & P2D) AND (P3N & P3D)
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts
Posted - 2011-07-22 : 09:07:25
Would union all work for you - like this:
SELECT ID, P1N, P1D WHERE P1N <> '000' AND P1D <> '000' UNION ALLSELECT ID, P2N, P2D WHERE P2N <> '000' AND P2D <> '000' UNION ALLSELECT ID, P3N, P3D WHERE P3N <> '000' AND P3D <> '000'