61,817
社区成员




供应信息表一些字段:
ID(主键) Val(有效期 int 值:10,20,30,90)
IsCheck(审核状态 int,1代表审核通过 0审核中 2代表审核不通过) PubTime(信息发布时间)
现在要查询每种审核状态共有多少条纪录?审核通过纪录分成已过期跟未过期
例结果:
ISCheck Cou --如无纪录则用0代替,就是说有可能审核不通过纪录为0,但也要显示出来,返回0
0 10 --审核中纪录(所有纪录)
1 300 --审核通过纪录,不包括已过期
2 20 --审核未通过纪录(所有纪录)
4 50 --过期纪录数(审核通过纪录中过期纪录 4为自已写值,数据库中不存在 )
过期就是指当前时间-发布时间>有效期天数
笨点的方法就是select count(*) from .. where ..一条一条查询出来,但我想知道有没有办法直接查询出来
select 0 as ischeck,count(1) as cou from tb where ischeck=0
union all
select 1 ,sum(case when dateadd(dd,val,pubtime)>getdate() then 1 else 0 end) from tb where ischeck=1 --未过期=发布时间+天数 大于当天就没有过期
union all
select 2,count(1) from tb where ischeck=2
union all
select 4 ,sum(case when dateadd(dd,val,pubtime)<getdate() then 1 else 0 end) from tb where ischeck=1 --已过期=发布时间+天数 大于当天就没有过期,不知道算不算当天