I have a table with values ββas follows -
EMP_CODE | LEAVENAME | APP_TYPE | LEAVE_DATE | ACT_DAYS -------------------------------------------------------- ST006 | CL | P | 2012-01-03 | 1.0 ST006 | CL | P | 2012-01-18 | 1.0 ST006 | SL | P | 2012-01-27 | 1.0 ST002 | CL | P | 2012-01-04 | 1.0 ST002 | CL | P | 2012-01-12 | 1.0 ST002 | SL | P | 2012-01-27 | 1.0 OCO038 | CL | P | 2012-01-27 | 1.0 HO188 | CL | P | 2012-01-09 | 1.0 HO188 | CL | P | 2012-01-30 | 1.0 HO085 | CL | P | 2012-01-19 | 1.0 HO085 | SL | P | 2012-01-23 | 1.0
I wrote this query to summarize all vacation types as columns for each employee. Each employee should have only one line.
SELECT EMP_CODE,[CL],[LWP],[PL],[SL] FROM LEAVE_DETAIL L PIVOT (SUM(ACT_DAYS) FOR LEAVENAME IN ([CL],[LWP],[PL],[SL])) AS PVT ORDER BY EMP_CODE;
But this query does not give me the expected result. For each employee, there are several lines that are not what I want.
The following table shows the expected result -
EMP_CODE | CL | SL | ---------|------|-----| ST006 | 2.0 | 1.0 | ST002 | 2.0 | 1.0 | OCO038 | 1.0 | 0.0 | HO188 | 2.0 | 0.0 | HO085 | 1.0 | 1.0 |
Please, help.
source share