Solved

Using TSQL to Return Column Data as Rows

Posted on 2016-10-04
3
61 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 500 total points
ID: 41828452
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 41

Expert Comment

by:Sharath
ID: 41828540
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
ID: 41830185
This worked Great!
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

756 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