September 22, 2012 at 3:31 am
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
September 22, 2012 at 3:36 am
SWATH (9/22/2012)
I HAVE below TABLE with colum names proposer,approver,designationPROPOSER 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
September 22, 2012 at 5:48 am
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.
September 22, 2012 at 10:35 am
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]
kirankranthi,PHANI,ARUN,SANGEET,
SURYANULLNULLNULLMALATHI,SRIKANTH,
SWATHIMALATHI,PANKAJ,VARSHA,NULLNULL
September 23, 2012 at 1:24 pm
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
Change is inevitable... Change for the better is not.
September 24, 2012 at 11:14 pm
THANK YOU ALL:)
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply