Solved

unpivot

Posted on 2014-09-16
6
138 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
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.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

867 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

21 Experts available now in Live!

Get 1:1 Help Now