Solved

Using TSQL to Return Column Data as Rows

Posted on 2016-10-04
3
67 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
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.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

628 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