我有一個查詢,我正在運行查找特定團隊在過去一個月中輸入的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
我試圖使之成爲一個子查詢的一部分,我想我的問題是我無法包裝我的頭找出我需要做的事情以獲得我需要的結果。
任何幫助,非常感謝。
只是從你的'group by'中刪除這個'datediff(s,te.time_start,te.time_end)',並在'select'中爲該列添加'sum'。 – techspider