我在数据库中有服务请求。我需要知道过去 2 周内打开/关闭了多少天,按天分组。
我有这个查询:
SELECT RIGHT('00'+CONVERT(VARCHAR(2), DATEPART(MONTH, OPENTIME)), 2)
+ '-'
+ RIGHT('00'+CONVERT(VARCHAR(2), DATEPART(DAY, OPENTIME)), 2) AS 'Day',
COUNT(OPENTIME) AS
'Count of Opened',
COUNT(CLOSETIME) AS
'Count of Closed'
FROM JEZD.F_SERVICE_REQUESTS
WHERE OPENTIME >= DATEADD(DAY, DATEDIFF(DAY, 0,
DATEADD(D, -14, CURRENT_TIMESTAMP)), 0
)
GROUP BY RIGHT('00'+CONVERT(VARCHAR(2), DATEPART(MONTH, OPENTIME)), 2)
+ '-'
+ RIGHT('00'+CONVERT(VARCHAR(2), DATEPART(DAY, OPENTIME)), 2)
这是按 OpenTime 分组的,因此例如今天没有打开请求的服务,此 SQL 不会计算任何已关闭的票证,直到打开一些新票证。我尝试将 where 子句和 group 子句从 OpenTime 修改为 Current_Time,但出现错误Each GROUP BY expression must contain at least one column that is not an outer reference.
您有任何解决方法吗?
截至目前的数据集:
Day Opened Closed
09-30 29 21
10-02 23 18
09-27 27 25
10-03 16 12
10-01 59 44
09-26 23 18
10-04 31 20
09-25 35 29
10-07 22 12