请教一个group by 问题

表a
date123         value
-----------------------
2007-07-05   1
2007-07-05   2

现在要求列出 2007-07-01到 2007-07-05的所有value的和
结果应该是
-------------------------
2007-7-1          0
2007-7-2          0
2007-7-3          0
2007-7-4          0
2007-7-5           3
但是怎么才能把前4行查询出来啊 表中没有这些天的? 帮忙啊
我也来说两句 查看全部回复

最新回复

  • fdc1012 (2008-7-21 10:12:41)

    select  date123,sum(value) from table group by date123
  • redneck_he (2008-7-21 10:30:13)

    你必须先构造一个日期,然后关联就可以了
    select b.mdate,sum(nvl(a.value,0)) from
    (select to_char(date123,'yyyy-mm-dd') date123,value from t1) a,
    (select to_char(to_date('2007-01-01','yyyy-mm-dd') + rownum,'yyyy-mm-dd')  mdate
       from dual connect by rownum < 10) b
    where b.mdate = a.date123(+)
    group by b.mdate

    如果你的date123是字符型,就没有必要再转换.
    select b.mdate,sum(nvl(a.value,0)) from t1  a,
    (select to_char(to_date('2007-01-01','yyyy-mm-dd') + rownum,'yyyy-mm-dd')  mdate
       from dual connect by rownum < 10) b
    where b.mdate = a.date123(+)
    group by b.mdate
  • sieper (2008-7-21 10:36:06)

    楼上兄弟 多谢 哈
  • apollofeier (2008-7-21 11:19:12)

    同意,构建辅助表,或由3楼说的。