Possible Duplicate:
Pivot data in T-SQL
please make needed changes in the query to create a pivot form display …
select COUNT(employeeid) as count_of_employees,Title
from HumanResources.Employee
group by Title order by no_of_employees desc
this query is returning the following result
count_of_employees Title
26 Production Technician - WC50
26 Production Technician - WC60
26 Production Technician - WC40
25 Production Technician - WC30
22 Production Technician - WC20
17 Production Technician - WC10
15 Production Technician - WC45
14 Sales Representative
9 Buyer
5 Marketing Specialist
4 Scheduling Assistant
i need the desired result
Production Technician-WC50 | Production Technician-WC60 | Production Technician-WC40
26 | 26 | 26
To pivot the rows into columns the way you described, you have to use the
PIVOTtable operator. Something like so:SQL Fiddle Demo
But this is ugly, since you have to write these list of title. However, it is better to do this dynamically, by getting the list of titles dynamically like so:
SQL Fiddle Demo
This should give you:
This is nicer, but you might need to be careful with dynamic SQL, reed this for more details:
Note that: the table
YourQueryI used in my answer here, has to be replaced with your query: