我试图计算上周(周一至周日)的所有数据。但似乎由于某种原因它忽略了星期天,这样我就得不到正确的数:
SET DATEFIRST 1
Select *
FROM Products a
inner join Locations b
on a.Location = b.LocID
where b.LocID = 12 AND
a.Created >= DATEADD(week, DATEDIFF(week,0,GETDATE())-1,-1)
AND a.Created < DATEADD(week, DATEDIFF(week,0,GETDATE()),-1)
答案 0 :(得分:0)
尝试使用星期几。我认为你可能在使用WEEK时遇到麻烦,因为你实际需要计算:
其中a.Created&gt; =(上周星期一)和a.Created&lt; (本周一)
以下是使用DW的查询:
SET DATEFIRST 1
Select *
FROM Products a
inner join Locations b
on a.Location = b.LocID
where b.LocID = 12 AND
a.Created >= DATEADD(day, -DATEPART(dw, GETDATE())-6, CONVERT(DATE,GETDATE()))
AND a.Created < DATEADD(day, -DATEPART(dw, GETDATE())+1, CONVERT(DATE,GETDATE()))