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


NEED HELP IN PIVOT


NEED HELP IN PIVOT

Author
Message
SWATH
SWATH
SSC Rookie
SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)

Group: General Forum Members
Points: 45 Visits: 171
I HAVE below TABLE with colum names proposer,approver,designation


PROPOSER APPROVER DESIGNATION
----------------------------------
kiran ARUN CFO
kiran kranthi HOD
kiran PHANI CEO
kiran SANGEET ACCOUNT
SURYA MALATHI ACCOUNT
SURYA SRIKANTH ACCOUNT
SWATHI MALATHI HOD
SWATHI PANKAJ CEO
SWATHI VARSHA CEO


and i need O/P like

proposer HOD CEO CFO ACCOUNT
-----------------------------------------------------------------------------------
kiran KRANTHI PHANI ARUN SANGEETH
SURYA -------- ----- ----- MALATHI,SRIKANTH
SWATHI MALATHI PANKAJ,VARSHA ----- -----------


BY USING PIVOT I CAN GET ONLY ONE VALUE



SELECT PROPOSER,[HOD],[CEO],[CFO],[ACCOUNT]
FROM
(SELECT PROPOSER,DESIGNATION,APPROVER FROM KIRAN)A
PIVOT
(MAX(APPROVER) FOR DESIGNATION IN([HOD],[CEO],[CFO],[ACCOUNT]))PVT


HOW CAN I GET 2 VALUES ??????????


THANKS IN ADVNACE
SWATH
SWATH
SSC Rookie
SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)

Group: General Forum Members
Points: 45 Visits: 171
SWATH (9/22/2012)
I HAVE below TABLE with colum names proposer,approver,designation


PROPOSER APPROVER DESIGNATION
----------------------------------
kiran ARUN CFO
kiran kranthi HOD
kiran PHANI CEO
kiran SANGEET ACCOUNT
SURYA MALATHI ACCOUNT
SURYA SRIKANTH ACCOUNT
SWATHI MALATHI HOD
SWATHI PANKAJ CEO
SWATHI VARSHA CEO


and i need O/P like

proposer HOD CEO CFO ACCOUNT
-----------------------------------------------------------------------------------
kiran KRANTHI PHANI ARUN SANGEETH
SURYA -------- ----- ----- MALATHI,SRIKANTH
SWATHI MALATHI PANKAJ,VARSHA ----- -----------


BY USING PIVOT I CAN GET ONLY ONE VALUE



SELECT PROPOSER,[HOD],[CEO],[CFO],[ACCOUNT]
FROM
(SELECT PROPOSER,DESIGNATION,APPROVER FROM KIRAN)A
PIVOT
(MAX(APPROVER) FOR DESIGNATION IN([HOD],[CEO],[CFO],[ACCOUNT]))PVT


HOW CAN I GET 2 VALUES ??????????


THANKS IN ADVNACE

Greg Snidow
Greg Snidow
SSCarpal Tunnel
SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)

Group: General Forum Members
Points: 4349 Visits: 2494
Swath, I don't have access to a server right now, but if you search for PIVOT there are countless examples here, and also on google.

Greg
_________________________________________________________________________________________________
The glass is at one half capacity: nothing more, nothing less.
ssurve
ssurve
SSC-Enthusiastic
SSC-Enthusiastic (124 reputation)SSC-Enthusiastic (124 reputation)SSC-Enthusiastic (124 reputation)SSC-Enthusiastic (124 reputation)SSC-Enthusiastic (124 reputation)SSC-Enthusiastic (124 reputation)SSC-Enthusiastic (124 reputation)SSC-Enthusiastic (124 reputation)

Group: General Forum Members
Points: 124 Visits: 16
Try this


declare @Tmp table(PROPOSER varchar(100),APPROVER varchar(100),DESIGNATION varchar(100))

insert into @Tmp
select 'kiran','ARUN','CFO' union all
select 'kiran','kranthi','HOD' union all
select 'kiran','PHANI','CEO' union all
select 'kiran','SANGEET','ACCOUNT' union all
select 'SURYA','MALATHI','ACCOUNT' union all
select 'SURYA','SRIKANTH','ACCOUNT' union all
select 'SWATHI','MALATHI','HOD' union all
select 'SWATHI','PANKAJ','CEO' union all
select 'SWATHI','VARSHA','CEO'

SELECT PROPOSER,[HOD],[CEO],[CFO],[ACCOUNT]
FROM
(select PROPOSER,DESIGNATION,(select APPROVER+',' as[text()] from @Tmp [1] where [1].PROPOSER=[2].PROPOSER and [1].DESIGNATION=[2].DESIGNATION for XML path('')) as APPROVER
from @Tmp [2])A
PIVOT
(MAX(APPROVER) FOR DESIGNATION IN([HOD],[CEO],[CFO],[ACCOUNT]))PVT


Output
PROPOSER [HOD] [CEO] [CFO] [ACCOUNT]
kiran kranthi, PHANI, ARUN, SANGEET,
SURYA NULL NULL NULL MALATHI,SRIKANTH,
SWATHI MALATHI, PANKAJ,VARSHA, NULL NULL
Dave Ballantyne
Dave Ballantyne
SSCertifiable
SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)

Group: General Forum Members
Points: 7172 Visits: 8370
Try a 'manual pivot' like i show in this article http://www.sqlservercentral.com/articles/T-SQL/73887/



Clear Sky SQL
My Blog
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (215K reputation)SSC Guru (215K reputation)SSC Guru (215K reputation)SSC Guru (215K reputation)SSC Guru (215K reputation)SSC Guru (215K reputation)SSC Guru (215K reputation)SSC Guru (215K reputation)

Group: General Forum Members
Points: 215526 Visits: 41979
To clean up the presence of trailing commas, use STUFF on ssurve's code like the following...

 SELECT PROPOSER,HOD,CEO,CFO,ACCOUNT
FROM ( --=== This concatenates multiple Approver values.
SELECT PROPOSER,
DESIGNATION,
APPROVER = STUFF((SELECT ',' + APPROVER
FROM @Tmp t1
WHERE t1.PROPOSER = t2.PROPOSER
AND t1.DESIGNATION = t2.DESIGNATION
ORDER BY t1.PROPOSER
FOR XML PATH('')),1,1,'')
FROM @Tmp t2
) d
PIVOT (MAX(APPROVER) FOR DESIGNATION IN(HOD,CEO,CFO,ACCOUNT))PVT
;




Output:
PROPOSER HOD     CEO           CFO  ACCOUNT
-------- ------- ------------- ---- ----------------
kiran kranthi PHANI ARUN SANGEET
SURYA NULL NULL NULL MALATHI,SRIKANTH
SWATHI MALATHI PANKAJ,VARSHA NULL NULL


--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
SWATH
SWATH
SSC Rookie
SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)

Group: General Forum Members
Points: 45 Visits: 171
THANK YOU ALLSmile
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