Solved

Using TSQL to Return Column Data as Rows

Posted on 2016-10-04
3
40 Views
Last Modified: 2016-10-05
Thank you all in advance!

I have table: TableA

RowID Greeting  IDINS  VerifyApt
108            y            y              y
109            y            n              y
110            y          OOS          y

I would like to create a new table, TableB from the data in TableA that looks like:

ROWID        Step           DATA
108          Greeting            y
108             IDNS               y
108         VerifApt              y
109         Greeting             y
109             IDNS                n
109          VerifyApt            y
110          Greeting            y
110              IDNS               y
110         VerifyApt            y


Is this a PIVOT? UNPIVOT? The SQL I found kept referring to an aggregate function but as you can see, I dont need one.

Thank you all again.
0
Comment
Question by:Victor Nares
3 Comments
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 500 total points
Comment Utility
I'd use CROSS APPLY for this:

SELECT
    RowID,
    which_column AS Step,
    CASE which_column
    WHEN 'Greeting' THEN Greeting
    WHEN 'IDINS' THEN IDINS
    WHEN 'VerifyApt' THEN VerifyApt
    ELSE '?'
    END AS DATA
FROM /*your_table_name*/ (
    VALUES(108,            'y',            'y',              'y'),
    (109 ,           'y' ,           'n',              'y'),
    (110,            'y'  ,        'OOS',          'y')
) AS your_sample_data(RowID, Greeting, IDINS, VerifyApt)
CROSS APPLY (
    VALUES('Greeting'),('IDINS'),('VerifyApt')
) AS which_columns(which_column)
0
 
LVL 40

Expert Comment

by:Sharath
Comment Utility
try UNPIVOT
declare @table table (RowID int, Greeting varchar(1), IDINS varchar(1), VerifyApt varchar(1))
insert @table values (108,'y','y','y'),(109,'y','n','y'),(110,'y','y','y')

select RowID, Step, DATA
  from @table
unpivot (DATA for Step IN (Greeting,IDINS,VerifyApt)) p
/*
RowID	Step	DATA
108	Greeting	y
108	IDINS	y
108	VerifyApt	y
109	Greeting	y
109	IDINS	n
109	VerifyApt	y
110	Greeting	y
110	IDINS	y
110	VerifyApt	y
*/

Open in new window

1
 

Author Closing Comment

by:Victor Nares
Comment Utility
This worked Great!
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how the fundamental information of how to create a table.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now