SQL 动态 where语句(case when)

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)

上一篇:SQL Server按时间分段统计数据


下一篇:如何在PHP中获得两天之间的差异