select * from tablename
where time>= case when DATEDIFF(DAY,DATEADD(week,DATEDIFF(week,0,getdate()),0),GETDATE())=0
then DATEADD(week,-1,DATEADD(week,DATEDIFF(week,0,GETDATE()),0))
else DATEADD(week,DATEDIFF(week,0,getdate()),0)
end
逻辑 :如果当前是周一,就查询上周一以来的数据,否则就查询本周数据;
//上周一
select DATEADD(week,-1,DATEADD(week,DATEDIFF(week,0,GETDATE()),0))
//本周一
select DATEADD(week,DATEDIFF(week,0,getdate()),0)