Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Using TSQL to Return Column Data as Rows

Posted on 2016-10-04
3
Medium Priority
?
76 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 70

Accepted Solution

by:
Scott Pletcher earned 2000 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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

722 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