我编写了以下SQL
查询,该查询提供每位员工total
天数。
SELECT DISTINCT
([Entered_By]),
SUM(ALL DATEDIFF(DAY, Time_End_UTC, Date_Entered_UTC)) OVER (PARTITION BY entered_by) AS Days
FROM [dbo].[Time_Entry]
WHERE date_start BETWEEN '06-01-2015' AND '06-30-2015'
AND DATEDIFF(DAY, Time_End_UTC, Date_Entered_UTC) > 2
我也可以改变" sum
"到" count
" (并删除" all
")并获取总记录数。
但是我需要以下其中一项:
当我尝试同时拥有(option 1)
时,我遇到了问题。我无法弄清楚如何让multiplication
(option 2)
发挥作用。
感谢您的帮助。
答案 0 :(得分:2)
我觉得使用GROUP BY
查询而不是窗口sum
更容易实现这一点:
Select [Entered_By]
,sum(datediff(Day, Time_End_UTC, Date_Entered_UTC))
,count(*) --count of all records in the group, which you could also multiply by the sum if you prefer option 2
From [dbo].[Time_Entry]
where date_start between '06-01-2015' and '06-30-2015'
and datediff(DAY, Time_End_UTC, Date_Entered_UTC) >2
GROUP BY [Entered_By] --this takes care of the distinctness of [Entered_By] and partitioning you had in your original query
答案 1 :(得分:0)
对于选项2,您可以
SELECT COUNT(ATTRIB) * SUM(ATTRIB) FROM TABLE;