I have a Table1
shown here:
;WITH Table1 AS (
SELECT *
FROM (VALUES
(164021, 'Balajee', 100, 100, 'YTD'),
(164021, 'Balajee', 200, 100, 'YTD'),
(164021, 'Balajee', 400, 300, 'MTD'),
(164021, 'Balajee', 200, 100, 'MTD')
) as t (EmpID, [Emp Name], GROSS_PAY, NET_PAY, DUR_TYPE)
)
This table has column called duration type which can have value as YTD
or MTD
. Now I want my table after doing a select using Pivot to look like the one shown in the table below. Can someone help me to do this? I'm very new to this Pivot and need some help.
EmpID Empname Grosspay_ytd net_pay_ytd Grosspay_mtd netpay_mtd
------- ------- --------------- ----------- --------------- -----------
164021 Balajee 300 200 600 400
You can use the PIVOT function to get the result however part of the problem is that you have the two columns that you want to pivot in two columns.
Since your data to pivot is in two columns there are a few ways that you can get the result. You can use an aggregate function with a CASE expression:
select empid, empname,
sum(case when dur_type='YTD' then gross_pay else 0 end) gross_pay_ytd,
sum(case when dur_type='YTD' then net_pay else 0 end) net_pay_ytd,
sum(case when dur_type='MTD' then gross_pay else 0 end) gross_pay_mtd,
sum(case when dur_type='MTD' then net_pay else 0 end) net_pay_mtd
from yt
group by empid, empname;
If you want to use the PIVOT function then you can UNPIVOT the data in the gross_pay
and net_pay
columns so the data exists in one column and then apply the PIVOT function. Since you are using SQL Server 2008 you can unpivot the data using CROSS APPLY with a VALUES clause:
select empid, empname, col+'_'+dur_type as col, value
from yt
cross apply
(
values
('gross_pay', gross_pay), ('net_pay', net_pay)
) c (col, value)
See Demo . This will convert the current data into the result:
| EMPID | EMPNAME | COL | VALUE |
--------------------------------------------
| 164021 | Balajee | gross_pay_YTD | 100 |
| 164021 | Balajee | net_pay_YTD | 100 |
| 164021 | Balajee | gross_pay_YTD | 200 |
| 164021 | Balajee | net_pay_YTD | 100 |
As you can see, we know have multiple rows for each employee with a column that contains the names of the columns you want in the final pivot result. When you apply the PIVOT function the code will be:
select empid, empname,
gross_pay_ytd, net_pay_ytd, gross_pay_mtd, net_pay_mtd
from
(
select empid, empname, col+'_'+dur_type as col, value
from yt
cross apply
(
values
('gross_pay', gross_pay), ('net_pay', net_pay)
) c (col, value)
) d
pivot
(
sum(value)
for col in (gross_pay_ytd, net_pay_ytd, gross_pay_mtd, net_pay_mtd)
) piv;
See SQL Fiddle with Demo . Both this version and the CASE version will give a result:
| EMPID | EMPNAME | GROSS_PAY_YTD | NET_PAY_YTD | GROSS_PAY_MTD | NET_PAY_MTD |
--------------------------------------------------------------------------------
| 164021 | Balajee | 300 | 200 | 600 | 400 |
Try this sql.
select empid,empname,sum(case dur_type when dur_type='YTD' then gross_pay else 0 END)as grosspay_ytd,
sum(case when dur_type='MTD' then gross_pay else 0 END) as grosspay_mtd ,
sum(case when dur_type='YTD' then net_pay else 0 END) as netpay_ytd,
sum(case when dur_type='MTD' then net_pay else 0 END) as netpay_mtd
from employee
group by empid,empname,dur_type
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.