Solved

unpivot

Posted on 2014-09-16
6
143 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
[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
6 Comments
 
LVL 69

Expert Comment

by:Scott Pletcher
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
Percona Monitoring and Management and Grafana

Proactive monitoring is vital to a highly-available environment. We have a quick start guide on Experts Exchange for Grafana users.

 
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 49

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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
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 to return specific rows and columns, with various degrees of sorting and limits in place.

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