在oracle 11g上自学SQL,并使用员工信息数据库。
我正在尝试返回每个“办公室”员工的工作小时数和天数
但是,“工作时间”会返回每个员工轮班的正确时间,但是作为单独的行,而不是一行包含总天数和总小时数。 (这意味着每行的天数= 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"
我愿意回答任何需要的问题,但我根本无法理解为什么不为每个办公地点的每位员工分成一行
答案 0 :(得分:2)
试试这个......
在SELECT:
中放置一个Sum()round((shift2.MaxSignOffTime-shift.MinSignOnTime)*24, 1)
并删除此内容:
(shift2.MaxSignOffTime-shift.MinSignOnTime)
来自GROUP BY子句的