0
在oracle 11g上自學SQL,使用員工信息db。自學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"
我願意回答所需的任何問題,但我根本無法看到它爲什麼不分組到一行爲每個員工每個辦公地點
你能不能做一個簡單的測試案例?學習抽象/簡化問題也是一個很好的習慣(我至少沒有能力學習所有這些冗長的查詢......) –