1.依次查询 最近七天星期几
select datename(weekday,getdate()-6) as a,
datename(weekday,getdate()-5) as b,
datename(weekday,getdate()-4) as c,
datename(weekday,getdate()-3) as d,
datename(weekday,getdate()-2) as e,
datename(weekday,getdate()-1) as f,
datename(weekday,getdate()-0) as g
with t as (
select convert(varchar(10),dateadd(DAY,t2.number,t1.day),120) date
from (select substring(convert(varchar,dateadd(month,-0,getdate()),120),1,7)+'-01' day) t1,
(select number from MASTER..spt_values WHERE TYPE='P' AND number>=0 and number<=31) t2
where convert(varchar(10),dateadd(DAY,t2.number,t1.day),120) like
substring(convert(varchar,dateadd(month,-0,getdate()),120),1,7)+'%')
select id=ROW_NUMBER()OVER(ORDER BY t1.date),t1.date,
(select count(*) from tb_voice_info where substring(convert(varchar,oper_date,120),1,11)=t1.date) as count ,
(select count(*) from tb_voice_info where substring(convert(varchar,oper_date,120),1,11)=t1.date and voice_state=0) as loseorder ,
(select count(*) from tb_voice_info where substring(convert(varchar,oper_date,120),1,11)=t1.date and voice_state=2) as getorder
from t t1 where t1.date <= convert(varchar(10),getdate(),23)
(查询上个月convert(varchar,dateadd(month,-1,getdate()),120))
执行结果:
3.查询最近一周星期几 几号 数据汇总 按倒序
with t as (
select datename(weekday,getdate()- t1.number) weeknum
,substring(convert(varchar,getdate()- t1.number,120),1,11) daynum
from (select number from MASTER..spt_values WHERE TYPE='P' AND number >=0 and number <=6 ) t1 )
select weeknum,daynum,
(select count(*) from tb_voice_info where substring(convert(varchar,oper_date,120),1,11)=daynum) as count ,
(select count(*) from tb_voice_info where substring(convert(varchar,oper_date,120),1,11)=daynum and voice_state=0) as loseorder ,
(select count(*) from tb_voice_info where substring(convert(varchar,oper_date,120),1,11)=daynum and voice_state=2) as getorder
from t