Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


NEED HELP IN PIVOT


NEED HELP IN PIVOT

Author
Message
SWATH
SWATH
Grasshopper
Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)

Group: General Forum Members
Points: 17 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
Grasshopper
Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)

Group: General Forum Members
Points: 17 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
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1611 Visits: 2478
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 (112 reputation)SSC-Enthusiastic (112 reputation)SSC-Enthusiastic (112 reputation)SSC-Enthusiastic (112 reputation)SSC-Enthusiastic (112 reputation)SSC-Enthusiastic (112 reputation)SSC-Enthusiastic (112 reputation)SSC-Enthusiastic (112 reputation)

Group: General Forum Members
Points: 112 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
SSCommitted
SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

Group: General Forum Members
Points: 1818 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-Forever
SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)

Group: General Forum Members
Points: 44948 Visits: 39859
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.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is usually not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
SWATH
SWATH
Grasshopper
Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)

Group: General Forum Members
Points: 17 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