I'm attempting to Sum (Present Hours), (Sickness Hours) by Department and month. This sum must include the department they work in which I've done by including a case statement. I've attempted this myself multiple times however, I can't seem to get it to work as expected.
EMP_NO Wage_type Hours_worked Department OBJID(UNIQUE) DATE
10011 Normal 14 1063 ABC116 04/01/18
10011 Normal 07 1063 ABC117 05/01/18
10011 SICK 21 1063 ABC118 01/02/18
10030 Normal 12.5 1054 ABC119 02/02/18
10030 SICK 7 1054 ABC120 03/02/18
10030 SICK 7 1054 ABC121 04/02/18
Example code for raw data etc:
Select
TPDRS.OBJID
,TPDRS.WAGE_HOURS
,(case
when TPDRS.ORG_CODE = 1010 then 'Trolley P1'
when TPDRS.ORG_CODE = 1011 then 'Trolley P2'
when TPDRS.ORG_CODE = 1053 then 'Trolley P3'
when TPDRS.ORG_CODE between 1054 and 1057 then 'Trolley P4'
when TPDRS.ORG_CODE between 1040 and 1047 then 'Trolley P5'
when TPDRS.ORG_CODE in ('1063','1064','1065','1068') then 'Trolley P6'
else 'NOT REQUIRED'
end) as Department
,TPDRS.EMP_NO
,TPDRS.ACCOUNT_DATE
,EXTRACT(Month from TPDRS.ACCOUNT_DATE) "Month_Number"
,EXTRACT(Year from TPDRS.ACCOUNT_DATE) "Year"
from TrolleyParkAttendant TPDRS
WHERE TPDRS.ACCOUNT_DATE > '01/DEC/2017'
AND (TPDRS.WAGE_GRP_DB IN ( 'O', 'N' ) or WAGE_CODE = 'SICK')
Expected Results:
Department HoursSick HoursPresent Month
Trolley P6 0 21 01
Trolley P6 21 0 02
Trolley P3 14 12.5 02
Any assistance would be appreciated
sum(case when <condition> then hours_worked)for each column you're expecting to output the sum of, plus a group by on the other columns being selected, presumably.account_date > '01/DEC/2017'). The DBMS will try to convert the string to a date which will succeed or fail (with an exception or a wrong date) depending on session settings. Use ANSI date literals instead:account_date > DATE '2017-12-01'.