LearnItFirst User Forum

SQL Server 2008 SSAS Training Videos
Welcome Guest Search | New Posts | Members | Log In | Register

How to change some columns into rows Options
sqlmind
Posted: Monday, June 28, 2010 7:41:07 PM
Rank: Newbie

Joined: 6/28/2010
Posts: 1
Points: 3
Where do you live?: Australia
Hi,

I've a query in which i need to change some of the columns into rows.
Please see the example;

Existing query

[Emp_Name] [PHN] [MOB] [OTH]
[Steve] [223] [456] [895]
[John] [789] [789] [421]
[jack] [ ] [252] [252]


I need this query to be changed in a format like this..
[Emp_Name] [NMBR] [TYP]
[Steve] [223] [P]
[Steve] [456] [M]
[Steve] [895] [O]
[John] [789] [P+M]
[John] [421] [O]
[jack] [252] [M+O]



Please let me know that if anyone got logic..

Regards

Robz
Scott Whigham
Posted: Tuesday, June 29, 2010 6:01:00 AM


Rank: Super Mod

Joined: 3/20/2006
Posts: 476
Points: 1,053
Where do you live?: Dallas, TX
You would need separate queries for that cobbled together with a UNION - something like:

SELECT Emp_Name, PHN AS NMBR, 'P' AS TYP
FROM TheTable t1
UNION
SELECT Emp_Name, MOB AS NMBR, 'M' AS TYP
FROM TheTable t1
UNION
SELECT Emp_Name, OTH AS NMBR, 'O' AS TYP
FROM TheTable t1
ORDER BY Emp_Name
Users browsing this topic
Guest


Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.