为了工资单的目的,我试图从上周五中午12点到本周五中午12点将所有记录提交给SQL Server。我过去7天的工作时间如下所示,但我需要特别针对上周五中午12点到本周五下午12点。
SELECT DISTINCT
notifications.requestid,
Max(timestatuschange),
assignedtouser,
Count(requestid) AS Total
FROM
notifications
WHERE
timestatuschange >= Dateadd(day, -7, Getdate())
AND notifications.status = 'Yellow'
OR timestatuschange >= Dateadd(day, -7, Getdate())
AND notifications.status = 'Red'
OR timestatuschange >= Dateadd(day, -7, Getdate())
AND notifications.status = 'Blue'
GROUP BY
requestid,
assignedtouser
答案 0 :(得分:1)
由于您使用GETDATE()
,我假设今天是星期五,您可以尝试这样的事情
SELECT DATEADD(hour,12,CONVERT(VARCHAR(10),GETDATE(),112)),
DATEADD(day,-7,DATEADD(hour,12,CONVERT(VARCHAR(10),GETDATE(),112)))
您的查询将是
DECLARE @StartDate DATETIME = DATEADD(day,-7,DATEADD(hour,12,CONVERT(VARCHAR(10),GETDATE(),112)))
DECLARE @EndDate DATETIME = DATEADD(hour,12,CONVERT(VARCHAR(10),GETDATE(),112))
SELECT DISTINCT notifications.requestid,
Max(timestatuschange),
assignedtouser,
Count(requestid) AS Total
FROM notifications
WHERE timestatuschange >= @StartDate
AND timestatuschange <= @EndDate
AND notifications.status IN ('Yellow','Red','Blue')
GROUP BY requestid,
assignedtouser