2016-08-26 138 views
1

我試圖計算完成了多少工作,無論工作人員在工作。我需要做的是過濾掉任何重複的內容,結果是整行不重複,因此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 

非常感謝幫助。

+0

員工101比102更受歡迎嗎?他們的「1」怎麼沒有計入該Jobcode /類型的總數?這裏的邏輯是什麼? – JNevill

+1

@SeanLange請不要只是將我重定向到不同論壇的主頁,這根本就不是一個有用的答案。在這裏張貼之前我已經搜索了幾個地方,只是沒有找到我正在尋找的東西。 – aantiix

+0

哎呀....那是因爲我發佈了錯誤的鏈接。 http://spaghettidba.com/2015/04/24/how-to-post-a-t-sql-question-on-a-public-forum/ –

回答

2

我認爲,一個快速的子查詢,你抓住每一天的最大,你的關鍵組,再總結的結果,會做的伎倆:

SELECT 
    jobcode, 
    jobtype, 
    sum(monday+tuesday+wednesday+thursday+friday+saturday+sunday) AS total 
FROM 
    (
     SELECT 
      jobcode, 
      jobtype, 
      tasktime, 
      max(m) as monday, 
      max(tu) as tuesday, 
      max(w) as wednesday, 
      max(th) as thursday, 
      max(f) as friday, 
      max(sa) as saturday, 
      max(su) as sunday 
     FROM Table1 T1 
     GROUP BY jobcode, jobtype, tasktime 
    ) t2 
GROUP BY jobcode, jobtype 

有可能是一些比更雄辯那,但是這應該完成工作。

+0

那就是我需要的,謝謝! – aantiix

0

請嘗試下面的查詢。

;With cte1 
as 
(SELECT ROW_NUMBER()OVER(PArtition by t1.JobCode,t1.JobType order by t1.JobCode,t1.JobType) RNO,t1.JobCode, t1.JobType, 
    SUM(t1.M + t1.Tu + t1.W + t1.Th + t1.F + t1.Sa + t1.Su) OVER(partition by t1.JobCode,t1.JobType ORDER BY t1.JobCode,t1.JobType) 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) 
    )) 

    SELECT t1.JobCode, t1.JobType,Totals 
    FROM cte1 
    WHERE RNO=1 
0

基本上,您可以使用超級棒的ROW_NUMBER函數幷包裝查詢,以便有效地1)定義一個分組,並且2)僅採用每個組的第一行。仔細查看over (partition by ... order by ...)條款,瞭解如何控制分組和「排名」(您希望通過結果實現哪些分組)。

select JobCode, JobType, Totals 
from (
    SELECT t1.JobCode, t1.JobType, SUM(t1.M + t1.Tu + t1.W + t1.Th + t1.F + t1.Sa + t1.Su) as Totals 
      ,row_number() over (partition by t1.JobCode order by t1.JobType) as rseq 
    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 
) x 
where rseq = 1 --filter to keep only the "first" row (JobType) for each JobCode 

需要注意的是關於「你怎麼決定要保留哪一個」是有效的,而這個例子假設你想看到「每個JOBCODE一行」,並保持「第一JobType爲JOBCODE」的評論。如果你能更清楚地解釋你想要使用的邏輯,可以對此進行調整。