mmooqwsazx 2014-09-02 06:35 采纳率: 0%
浏览 2411

一个分组SQL,如何使在查询不到结果的时候,得到一条数据

现在有一张xx表,表里面有如下的数据,字段名为create_date(如下1)
我用
select create_date, count(0) from xx where create_date BETWEEN date('2014-08-01') AND date('2014-09-01') group by create_date
去做个分组统计,得到了(如下2)的数据,没有19号的数据
问题:如何才能使统计的时候,即使没有19号的数据,也能得到一个0呢。

1:
create_date
2014-08-18
2014-08-20
2014-08-20
2014-08-20
2014-08-18
2014-08-20
2014-08-21
2014-08-22
2014-08-22
2014-08-22
2014-08-22
2014-08-22
2014-08-22
2014-08-22

2:
2014-08-18 2
2014-08-20 4
2014-08-21 1
2014-08-22 7

  • 写回答

1条回答 默认 最新

  • --小F-- 2014-11-18 00:44
    关注

    用master..spt_values构造日期 然后与你的表LEFT JOIN就可以了

    declare @startdate datetime,@enddate datetime
    set @startdate='2014-08-01'
    set @enddate='2014-09-01'
    ;with f1 as
    (
    select convert(varchar(10),dateadd(day,number,@startdate),120) as create_date
    from
    master..spt_values
    where
    datediff(day,dateadd(day,number,@startdate), @enddate)>=0
    and number>=0
    and type='p'
    )

    select a.create_date, isnull(count(0),0) from xx as a inner join f as b on a.create_date=b.create_date where a.create_date BETWEEN date('2014-08-01') AND date('2014-09-01') group by a.create_date

    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图