我試圖計算完成了多少工作,無論工作人員在工作。我需要做的是過濾掉任何重複的內容,結果是整行不重複,因此DISTINCT在這裏不起作用。我想過濾掉是否有基於JobCode,JobType TaskTime和星期幾的任何重複。所以,我的表如下所示:返回唯一值
JobCode JobType TaskTime EmployeeID M Tu W Th F Sa Su
==================================================================
1800 1 06:49 101 1 1 1 1 1 0 0
1800 1 06:49 102 1 0 0 0 0 0 0
1800 1 07:04 101 1 1 1 1 1 0 0
1800 1 07:26 101 1 1 1 1 1 0 0
1800 1 07:49 101 1 1 1 1 1 0 0
1800 2 15:55 101 1 1 1 1 1 0 0
1800 1 16:20 101 1 1 1 1 1 0 0
1800 1 16:50 101 1 1 1 1 1 0 0
1800 2 16:55 101 1 1 1 1 1 0 0
我的SQL查詢是這樣
SELECT t1.JobCode, t1.JobType,
t1.M, t1.Tu, t1.W, t1.Th, t1.F, t1.Sa, t1.Su,
SUM(t1.M + t1.Tu + t1.W + t1.Th + t1.F + t1.Sa + t1.Su) as Totals
FROM Table1 AS t1
JOIN Table1 AS t1_overlap ON
t1_overlap.EmployeeID = t1.EmployeeID AND
t1_overlap.JobType = t1.JobType AND
t1_overlap.TaskTime = t1.TaskTime
AND
(
(t1.M = 1 AND t1_overlap.M = t1.M) OR
(t1.Tu = 1 AND t1_overlap.Tu = t1.Tu) OR
(t1.W = 1 AND t1_overlap.W = t1.W) OR
(t1.Th = 1 AND t1_overlap.Th = t1.Th) OR
(t1.F = 1 AND t1_overlap.F = t1.F) OR
(t1.Sa = 1 AND t1_overlap.Sa = t1.Sa) OR
(t1.Su = 1 AND t1_overlap.Su = t1.Su)
)
GROUP BY t1.JobCode, t1.JobType, t1.M, t1.Tu, t1.W, t1.Th, t1.F, t1.Sa, t1.Su
返回的數據是這樣的
JobCode JobType M Tu W Th F Sa Su Totals
==================================================
1800 1 1 0 0 0 0 0 0 1
1800 1 1 1 1 1 1 0 0 30
1800 2 1 1 1 1 1 1 1 10
我想看到的是唯一的值,所以我不希望第一行顯示這個工作只由M僱員102完成,因爲我已經看到員工101在同一天和同一時間在同一個工作上工作。因此,我希望看到的,而不是爲以下:
JobCode JobType M Tu W Th F Sa Su Totals
==================================================
1800 1 1 1 1 1 1 0 0 30
1800 2 1 1 1 1 1 0 0 10
真的,我並不需要看到一週的日子裏,我只是向他們展示在這裏,所以我可以看到最新被退回。我實際需要看到的輸出是JobCode,JobType和總計如下:
JobCode JobType Totals
======================
1800 1 30
1800 2 10
非常感謝幫助。
員工101比102更受歡迎嗎?他們的「1」怎麼沒有計入該Jobcode /類型的總數?這裏的邏輯是什麼? – JNevill
@SeanLange請不要只是將我重定向到不同論壇的主頁,這根本就不是一個有用的答案。在這裏張貼之前我已經搜索了幾個地方,只是沒有找到我正在尋找的東西。 – aantiix
哎呀....那是因爲我發佈了錯誤的鏈接。 http://spaghettidba.com/2015/04/24/how-to-post-a-t-sql-question-on-a-public-forum/ –