SQL Pivot Table

create table table_pivot
(
EmpID varchar(10),
Dept varchar(10)
)

insert into table_pivot
values('A101','IT')

insert into table_pivot
values('A102','IT')

insert into table_pivot
values('B101','Mech')

insert into table_pivot
values('C101','EEE')

insert into table_pivot
values('D101','CS')

insert into table_pivot
values('D102','CS')

insert into table_pivot
values('E101','Chem')

insert into table_pivot
values('F101','Aro')


select * from table_pivot


select EmpID,[IT],[Mech],[EEE],[CS],[Chem],[Aro]
from [dbo].[table_pivot]
pivot
(
count(Dept)
for [Dept] in ([IT],[Mech],[EEE],[CS],[Chem],[Aro])
) as P



Comments

Popular posts from this blog

Announcing backup and restore improvements for large datasets near the size limit

Enhanced refresh with the Power BI REST API is now generally available

What’s New in SQL Server 2014 since SQL Server 2008 R2