Monday, June 16, 2014

how to use PIVOT table in sql server

Note : Using this row data will be act as column as well as column will be fit as row value, for more details refer below example

Table Structure with data

;with cts  -- Used CTE
as
(
select customer,Bhoonja,Biscuits,DahiVada
from (select customer,product,qty from items) up
pivot
(sum (qty) for product in(Bhoonja,Biscuits,DahiVada))as pvt
)
select cts.customer,isnull(cts.Bhoonja,0) as Bhoonja,
ISNULL(cts.DahiVada,0) as DahiVada,isnull(cts.Biscuits,0) as Biscuits,
isnull(cts.Bhoonja,0) + ISNULL(cts.DahiVada,0) + isnull(cts.Biscuits,0) as Total 
from cts order by cts.customer

After CTE execute (with Pivot Table)


No comments:

Post a Comment