Click here to Skip to main content
15,921,646 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hi ,

I need some help in making a PIVOT query for the following situation:
My data looks like this:

    ID   Participant  Representative  Manager
1   100  Orange       Mango           Banana
2   200  Mango        Banana          Orange
3   300  Null         Null            Mango
4   400  Banana       Null            Null
5   500  Orange       Banana          Null



I want it the following way:

    ID   Participant-Orange Participant-Mango Participant-Banana....
1   100   1                 
2   200                          1                                 
3   300                    
4   400                                             1             
5   500   1             



and so on for the combinations of Representatives and Manager and their count below it for Orange,Banana,Mango..

Tried doin it using Pivot Query but that can only help me create headers this way..the count for all shows null which is not what i want.
Can someone please help me with the query to achieve this dataset.

[edit] removed 'Urgent!' from subject [/edit]

[edit]Code block added, "Ignore HTML..." option disabled, subject length increased to 30+ characters- OriginalGriff[/edit]
Posted
Updated 8-Mar-11 20:46pm
v3
Comments
OriginalGriff 9-Mar-11 2:47am    
How did you manage to shorten the subject below 30 chars? Just interested...
Albin Abel 9-Mar-11 2:51am    
Hi Original Griff, It is not below 30 chars, but having invisible chars, the space. :)

1 solution

If your table name is test and your primary key filed (unique values) is keyid then use

SQL
SELECT * FROM Test
 PIVOT (COUNT(keyid) FOR participant  IN ([Orrange],[Mango],[Banana])) p


in you case the first column seems unique. Use that to count.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900