Solved

unpivot

Posted on 2014-09-16
6
137 Views
Last Modified: 2014-09-30
Hi experts below is my original data
cust    code1   code2  tex1   tex2
 
123       1              2        Y       N

And I need to get results like this

cust    code    text
123      1           Y
123       2          N


But with my below query I am getting results as shown after query, please suggest

SELECT  Distinct
                           Customer_No,
                           Code,
                           TextMessage
     FROM   (SELECT DISTINCT
                 Customer_no,
                 Code1,Code2, text1,text2)                
          FROM   #final ) AS p UNPIVOT ( Code  For
         Col1  IN (Code1,Code2) ) AS unpvt1
         UNPIVOT ( TextMessage  For
         Col2  IN (text1,text2) ) AS unpvt2
       -- ) AS unpvt3
        where Right(Col1,1)=Right(col2,1)


cust     code  text
123        1        Y
123        1        N
0
Comment
Question by:sqlcurious
6 Comments
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 40326611
PIVOT is so MS-centric (and somewhat convoluted), so in this case personally I'd just use CROSS APPLY:


SELECT
    cust,
    Code,
    Text
FROM #final
CROSS APPLY (
    SELECT Code1 AS Code, Text1 AS Text
    UNION ALL
    SELECT Code2, Text2
) AS ca
ORDER BY
    cust, code
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 40326861
But with my below query I am getting results as shown after query, please suggest
Any chance you can post the real query.  Your results did not come from this query.  It does not even compile.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 40326895
But I suspect this is what you are looking for:
SELECT  cust,
        code,
        [text]
FROM    (
	SELECT	cust,
               code1,
               code2,
               text1,
               text2
       FROM	final) f 
	UNPIVOT ( Code FOR Codes IN (code1, code2) ) u1 
	UNPIVOT ( [Text] FOR Texts IN (text1, text2) ) u2
WHERE   RIGHT(Codes, 1) = RIGHT(Texts, 1) 

Open in new window

0
Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

 
LVL 75

Assisted Solution

by:Anthony Perkins
Anthony Perkins earned 250 total points
ID: 40326897
This is how I tested it:
DECLARE @final TABLE(cust integer, code1 integer, code2 integer, text1 char(1), text2 char(1))
INSERT @final (cust, code1, code2, text1, text2)
VALUES  (123, 1, 2, 'Y', 'N')

SELECT  cust,
        code,
        [text]
FROM    (
	SELECT	cust,
               code1,
               code2,
               text1,
               text2
       FROM	@final) f 
		UNPIVOT ( Code FOR Codes IN (code1, code2) ) u1 
		UNPIVOT ( [Text] FOR Texts IN (text1, text2) ) u2
WHERE   RIGHT(Codes, 1) = RIGHT(Texts, 1) 

Open in new window

And here is the output:
cust	code	text
123	1	Y
123	2	N

Open in new window

0
 
LVL 48

Accepted Solution

by:
PortletPaul earned 250 total points
ID: 40326964
>>"PIVOT is so MS-centric (and somewhat convoluted), so in this case personally I'd just use CROSS APPLY:"

PIVOT &/or UNPIVOT are used by more than one dbms vendor but I know of only MS who uses "apply" cross or outer; so I can't think of a more MS centric approach than cross apply

However I agree, I much prefer the directness of using CROSS APPLY for unpivots.

A small variation is to use VALUES where you can literally "see" the layout
SELECT
      cust
    , ca.Code
    , ca.Text
FROM #final
      CROSS APPLY (
            VALUES
                    (Code1, Text1)
                  , (Code2, Text2)
                  ) AS ca (Code, Text)
ORDER BY
      cust, code

Open in new window

0
 

Author Closing Comment

by:sqlcurious
ID: 40353127
thanks
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

757 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

16 Experts available now in Live!

Get 1:1 Help Now