SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Query help with unpivot qry


Query help with unpivot qry

Author
Message
s-sql
s-sql
Mr or Mrs. 500
Mr or Mrs. 500 (505 reputation)Mr or Mrs. 500 (505 reputation)Mr or Mrs. 500 (505 reputation)Mr or Mrs. 500 (505 reputation)Mr or Mrs. 500 (505 reputation)Mr or Mrs. 500 (505 reputation)Mr or Mrs. 500 (505 reputation)Mr or Mrs. 500 (505 reputation)

Group: General Forum Members
Points: 505 Visits: 492
Need a query help, on unpivoting columns
Have the data as below...

create table #Test
(itemNo int,
Cat1 int,
SubCat11 int,
SubCat12 int,
Cat2 int,
SubCat21 int,
Cat3 int,
SubCat31 int
)

---
Insert into #Test select 888,3,97,245,4,272,5,128
Insert into #Test select 999,1,20,200,3,211,6,455
--

Need the results as

Item CatID SubCatID
888 3 97
888 3 245
888 4 272
888 5 128
999 1 20
999 1 200
999 3 211
999 6 455


Tried the following query...

SELECT ItemNo,
CategoryID
,SubCatID
FROM
(
SELECT ItemNo,
Cat1,Cat2,Cat3,
Subcat11,Subcat12,Subcat21,Subcat31
FROM #Test
) Main
UNPIVOT
(
CategoryID FOR Categories IN (Cat1,Cat2,Cat3)
) Sup
UNPIVOT
(
SubCatID For SubCats IN (Subcat11,Subcat12,Subcat21,Subcat31)
) Ct
WHERE RIGHT(Categories,1) = RIGHT(SubCats,1)

The SubCatID is repeated for cat column combinations...showing wrong results...

Any help appreciated...
Luis Cazares
Luis Cazares
SSC-Dedicated
SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)

Group: General Forum Members
Points: 39737 Visits: 19802
There's an alternate method to UNPIVOT using CROSS APPLY, you could read about it on the following link to understand the code I'm posting.
http://www.sqlservercentral.com/articles/CROSS+APPLY+VALUES+UNPIVOT/91234/

SELECT itemNo AS Item,
Cat AS CatID,
SubCat AS SubCatID
FROM #Test
CROSS APPLY( VALUES
( Cat1, SubCat11),
( Cat2, SubCat21),
( Cat3, SubCat31))x( Cat, SubCat)
ORDER BY Cat, SubCat




Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
s-sql
s-sql
Mr or Mrs. 500
Mr or Mrs. 500 (505 reputation)Mr or Mrs. 500 (505 reputation)Mr or Mrs. 500 (505 reputation)Mr or Mrs. 500 (505 reputation)Mr or Mrs. 500 (505 reputation)Mr or Mrs. 500 (505 reputation)Mr or Mrs. 500 (505 reputation)Mr or Mrs. 500 (505 reputation)

Group: General Forum Members
Points: 505 Visits: 492
The query worked. Made a minor modification, and it gave me the desired results.


SELECT itemNo AS Item,
Cat AS CatID,
SubCat AS SubCatID
FROM #Test
CROSS APPLY( VALUES
( Cat1, SubCat11),
( Cat1, SubCat12),
( Cat2, SubCat21),
( Cat3, SubCat31))x( Cat, SubCat)
ORDER BY itemNo, Cat, SubCat



Luis Cazares
Luis Cazares
SSC-Dedicated
SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)

Group: General Forum Members
Points: 39737 Visits: 19802
I'm glad it worked. I missed the Subcat12, but if you found out how to modify it, it means that you at least understand how the query is created. Be sure to understand how it works for future maintenance or explanations to other programmers. ;-)


Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search