我希望有人能幫助我解決這個問題,我一直在嘗試不同的組合,但似乎無法得到正確的結果。子查詢不輸出正確的結果?
首先,我有3個表,類別,部門和主人,主人有FK。我想得到每個部門的總時間總和和部門類別總時間總和,以便我可以看到哪個部門有多少時間用於該類別,都應該按部門分組並且按正確的順序?
我可以得到結果,但不出來吧,這裏是我的代碼...
SELECT a.department, time_spent , COUNT(DISTINCT (a.department)), sum_quarantine FROM
(SELECT d.department, SUM(time_spent) as time_spent
FROM master as m
INNER JOIN department as d ON d.dept_id = m.dept_id GROUP BY d.department) AS a,
(SELECT d.department, c.category, SUM(time_spent) as sum_quarantine
FROM master as m
INNER JOIN category as c ON c.cat_id = m.cat_id
INNER JOIN department as d ON d.dept_id = m.dept_id
WHERE category = 'Quarantine' GROUP BY d.department) as b
GROUP BY b.department
你能告訴我們你得到的輸出和你所期望的輸出嗎?可能還有一些輸入數據呢? –