--查询近7天数据记录行数,没有返回0

select a.click_date,ISNULL(b.count,0) as count
from (
SELECT convert(varchar(10),getdate()-7,120) as click_date
union all
SELECT convert(varchar(10),getdate()-6,120) as click_date
union all
SELECT convert(varchar(10),getdate()-5,120) as click_date
union all
SELECT convert(varchar(10),getdate()-4,120) as click_date
union all
SELECT convert(varchar(10),getdate()-3,120) as click_date
union all
SELECT convert(varchar(10),getdate()-2,120) as click_date
union all
SELECT convert(varchar(10),getdate()-1,120) as click_date
) a left join (
select convert(varchar(10),TimeOver,120) as datetime, count(*) as count
from tableName(表名) where ChargeUId(字段)=45
group by convert(varchar(10),TimeOver,120)
) b on a.click_date = b.datetime;

 

--查询近七天的数据内容

select * from Project_Task where DateDiff(dd,AddTime,getdate())<=7 

 

 

--查询N天的数据内容

select * from Project_Task where DateDiff(dd,AddTime,getdate())<=N

 

posted on 2020-06-10 17:33  lwmwsh  阅读(620)  评论(0编辑  收藏  举报