2016-09-28 117 views
0

我有一個查詢,我正在運行查找特定團隊在過去一個月中輸入的ConnectWise時間條目。我想要做的是修改下面的查詢,以包含每張票只有一個結果,但只有當datediff(s, time_start, time_end)結果超過1800時。我試過的所有錯誤都出來了,所以我的唯一MWE是以下查詢:SQL Server:選擇子查詢結果的總和,ConnectWise數據庫

SELECT 
    service.SR_Service_RecID AS "Ticket #", 
    te.Date_Start AS "Time Entry Day", 
    m.first_name AS First, 
    m.last_name AS Last, 
    DATEDIFF(s, te.time_start, te.time_end) AS "Time (s)" 
FROM 
    dbo.Time_Entry te 
LEFT JOIN 
    dbo.Activity_Class activity ON activity.Activity_Class_RecID = te.Activity_Class_RecID 
LEFT JOIN 
    dbo.SR_Service service ON te.SR_Service_RecID = service.SR_Service_RecID 
LEFT JOIN 
    dbo.Member m ON m.Member_RecID = te.Member_RecID 
LEFT JOIN 
    dbo.SR_Team_Mbr member ON member.Member_RecID = M.Member_RecID 
LEFT JOIN 
    dbo.SR_Team T ON T.SR_Team_RecID = member.SR_Team_RecID 
WHERE 
    m.member_recID IN (SELECT M.Member_RecID 
         FROM Member M 
         LEFT JOIN dbo.SR_Team_Mbr member ON member.Member_RecID = M.Member_RecID 
         LEFT JOIN dbo.SR_Team T ON T.SR_Team_RecID = member.SR_Team_RecID 
         WHERE T.Description LIKE '%c' 
          OR T.Description LIKE '%d' 
          OR T.Description LIKE '%e') 
    AND activity.Description LIKE 'T%' 
    AND service.Date_Entered >= DATEADD(MONTH, -1, GETDATE()) 
GROUP BY 
    service.SR_Service_RecID, te.Time_RecID, te.Date_Start, 
    m.first_name, m.last_name, 
    DATEDIFF(s, te.time_start, te.time_end) 
ORDER BY 
    service.SR_Service_RecID DESC 

主要生產:

Ticket # Time Entry Day   First Last Time(s) 
756065  2016-09-28 00:00:00.000 Mr.  Smith 1,000 
756065  2016-09-28 00:00:00.000 Mr.  Smith 560 

我在尋找:

Ticket # Time Entry Day   First Last Time(s) 
756065  2016-09-28 00:00:00.000 Mr.  Smith 1,560 

我試圖使之成爲一個子查詢的一部分,我想我的問題是我無法包裝我的頭找出我需要做的事情以獲得我需要的結果。

任何幫助,非常感謝。

+1

只是從你的'group by'中刪除這個'datediff(s,te.time_start,te.time_end)',並在'select'中爲該列添加'sum'。 – techspider

回答

1

有兩件事情是,在你的查詢是不正確的:

  1. 你沒有在你的SELECT語句中的聚合函數。
  2. 你在你的group byTime (s)列導致附加行

因此,改變你的查詢,如下糾正這兩個問題:

SELECT 
    service.SR_Service_RecID AS "Ticket #", 
    te.Date_Start AS "Time Entry Day", 
    m.first_name AS First, 
    m.last_name AS Last, 
    SUM(DATEDIFF(s, te.time_start, te.time_end)) AS "Time (s)" 
FROM 
    dbo.Time_Entry te 
LEFT JOIN 
    dbo.Activity_Class activity ON activity.Activity_Class_RecID = te.Activity_Class_RecID 
LEFT JOIN 
    dbo.SR_Service service ON te.SR_Service_RecID = service.SR_Service_RecID 
LEFT JOIN 
    dbo.Member m ON m.Member_RecID = te.Member_RecID 
LEFT JOIN 
    dbo.SR_Team_Mbr member ON member.Member_RecID = M.Member_RecID 
LEFT JOIN 
    dbo.SR_Team T ON T.SR_Team_RecID = member.SR_Team_RecID 
WHERE 
    m.member_recID IN (SELECT M.Member_RecID 
         FROM Member M 
         LEFT JOIN dbo.SR_Team_Mbr member ON member.Member_RecID = M.Member_RecID 
         LEFT JOIN dbo.SR_Team T ON T.SR_Team_RecID = member.SR_Team_RecID 
         WHERE T.Description LIKE '%c' 
          OR T.Description LIKE '%d' 
          OR T.Description LIKE '%e') 
    AND activity.Description LIKE 'T%' 
    AND service.Date_Entered >= DATEADD(MONTH, -1, GETDATE()) 
GROUP BY 
    service.SR_Service_RecID, te.Date_Start, 
    m.first_name, m.last_name 
ORDER BY 
    service.SR_Service_RecID DESC 

MSDN article應該給你一個完整的洞察GROUP BY

+0

這給了我很奇怪的結果;我仍然得到每張票的多個行,並且時間(s)列不會正確地加起來 – jmd9qs

+1

您可能在'group by'中可能不需要其他列。嘗試從您的「group by」組中移除'te.Time_RecID'。我已經更新了我的答案,假設您不需要該專欄。 – techspider

+0

謝謝。這處理每張票的多行,但數學比以前更加關閉。即使只有一次入場的門票關閉 – jmd9qs

0

問題在於您加入SR_Team和SR_Team_Member表。一個會員可以在多個團隊中。看起來您正試圖檢查會員是否在特定的團隊中。

你提到你想要每張票有一行,但一個成員可以有多個時間入場每張票有相同的時間。所以在某些情況下,您將獲得每張票的多行。

如果您希望條目的所有成員的SUM超過1800秒,那麼您需要SUM DATEDIFF(),使用GROUP BY,從WHERE子句中刪除DATEDIFF()> 1800並在GROUP BY之後添加HAVING子句。

此查詢將讓你的任何個人時間條目>1800秒

SELECT 
    service.SR_Service_RecID AS "Ticket #", 
    convert(date,te.Date_Start) AS "Time Entry Day", 
    m.first_name AS First, 
    m.last_name AS Last, 
    DATEDIFF(s, te.time_start, te.time_end) AS "Time (s)" 
FROM 
    dbo.Time_Entry te 
INNER JOIN dbo.Activity_Class activity ON activity.Activity_Class_RecID = te.Activity_Class_RecID 
INNER JOIN dbo.SR_Service service ON te.SR_Service_RecID = service.SR_Service_RecID 
INNER JOIN dbo.Member m ON m.Member_RecID = te.Member_RecID 
WHERE datediff(s,te.time_start, te.time_end) > 1800 
AND activity.Description like 'T%' 
AND service.Date_Entered >= dateadd(month, -1, getdate()) 
AND EXISTS (select * 
      from dbo.SR_Team_Mbr tm 
      inner join dbo.SR_Team t on tm.SR_Team_RecID = t.SR_Team_RecID and tm.Member_RecID = m.Member_RecID 
      where (t.Description like '%c' or t.Description like '%d' or t.Description like '%e')) 
ORDER BY service.SR_Service_RecID desc 

此查詢將讓你門票,所有的時間項的總和上一票時間> 1800秒的成員

SELECT 
    service.SR_Service_RecID AS "Ticket #", 
    convert(date,te.Date_Start) AS "Time Entry Day", 
    m.first_name AS First, 
    m.last_name AS Last, 
    SUM(DATEDIFF(s, te.time_start, te.time_end))AS "Time (s)" 
FROM 
    dbo.Time_Entry te 
INNER JOIN dbo.Activity_Class activity ON activity.Activity_Class_RecID = te.Activity_Class_RecID 
INNER JOIN dbo.SR_Service service ON te.SR_Service_RecID = service.SR_Service_RecID 
INNER JOIN dbo.Member m ON m.Member_RecID = te.Member_RecID 
WHERE activity.Description like 'T%' 
AND service.Date_Entered >= dateadd(month, -1, getdate()) 
AND EXISTS (select * 
      from dbo.SR_Team_Mbr tm 
      inner join dbo.SR_Team t on tm.SR_Team_RecID = t.SR_Team_RecID and tm.Member_RecID = m.Member_RecID 
      where (t.Description like '%c' or t.Description like '%d' or t.Description like '%e')) 
GROUP BY service.SR_Service_RecID, convert(date,te.Date_Start), m.First_Name, m.Last_Name 
HAVING SUM(DATEDIFF(s, te.time_start, te.time_end)) > 1800 
ORDER BY service.SR_Service_RecID desc