【发布时间】:2016-05-29 15:10:19
【问题描述】:
我已经多次尝试了,但它似乎不起作用。这可能吗?以下是我目前的尝试:
With PrintsPerDayPerEmp(ID,[Month],[Day],[Year], [Number Of Prints])
AS
(Select ID,
datepart(month,Agent_Local_Time) as [Month],
datepart(day,Agent_Local_Time) as [Day],
datepart(year,Agent_Local_Time) as [Year],
count(RowID) as [Number of Prints]
From Table1
Where Agent_Local_Time >= (getdate()-90)
Group by datepart(day,Agent_Local_Time),
datepart(month,Agent_Local_Time),
datepart(year,Agent_Local_Time),
ID
)
select *
from PrintsPerDayPerEmp
where [Number Of Prints] > (sum([Number Of Prints])/90)*3
order by ID desc
【问题讨论】:
-
那么告诉我们你的
goes,你希望我们做什么? -
抱歉 :) 已添加。
-
我想这里的逻辑显然会给我整个列的总和作为一个数字,但我希望它显示找到的每个 ID 的总和......如果这有意义的话。跨度>
-
您能否详细说明“它似乎不起作用”?你有错误吗?错误的结果?
-
“打印总和”附近的语法不正确。我也不确定语法错误在哪里。如果我注释掉这些部分,它可以正常工作并返回过去 90 天内找到的每个 ID 的月、日、年、ID 和每天的打印计数。
标签: sql sql-server tsql common-table-expression