Teaching myself SQL, and using an employee info db, on oracle 11g.
I’m trying to return the total numbers of hours and days worked by an employee at each of the “offices”
However, the “work hours” returns the correct time of each employees shift, but as individual rows, instead of having one row with total number of days and total number of hours. (This means each row has number of days = 1)
SELECT L.OFFICE_NAME as "Shop",
(us.LAST_NAME || ' , ' || us.FIRST_NAME) AS "Employee Name",
COUNT(distinct shift.shift_date) as "WORK DAYS",
round((shift2.MaxSignOffTime-shift.MinSignOnTime)*24, 1) AS "WORK HOURS",
FROM
LOCAL_OFFICE L
JOIN
Orders ord
ON L.LOCAL_OFFICE_ID = ord.LOCAL_OFFICE_ID
JOIN
USERS us
ON
us.USER_ID = ord.ASSIGNED_TO_USER_ID
join
(SELECT min(act_sign_dt) as MinSignOnTime, USER_ID, shift_token
FROM CLIENT_SIGN
WHERE
BEG_OF_SHIFT = 'Y'
GROUP BY shift_date, user_id, shift_token) shift on shift. user_id = ord.assigned_to_user_id
join
(SELECT max(act_sign_dt) as MaxSignOffTime, USER_ID, shift_token
FROM CLIENT_SIGN
WHERE
end_OF_SHIFT = 'Y'
GROUP BY shift_date, user_id, shift_token) shift2 on shift2.shift_token = shift.shift_token
GROUP BY OFFICE_NAME, LAST_NAME, FIRST_NAME,shift.shift_date, (shift2.MaxSignOffTime-shift.MinSignOnTime)
ORDER BY OFFICE_NAME, "Employee Name"
I’m willing to answer any questions needed, but I simply cannot see why it is not grouping into one row for each employee for each office location
Try this…
Put a Sum() around this in the SELECT:
and remove this:
from the GROUP BY clause