select dat,sum(num) from
(
  select trunc(inputdate,'dd') dat,count(*) num from table_A 
  group by trunc(inputdate,'dd')
    union
  select trunc
  (
    (
      select min(trunc(inputdate,'dd')) from table_A
    )
    ,'dd'
  )+rownum-1 ,0 from all_tables 
  where rownum<
  (
    select ceil(max(inputdate)-min(inputdate)) from table_A
  )+1
)
group by dat
order by dat

解决方案 »

  1.   

    楼上的看不懂。
    加一条为0的结果集:
    select InputDate,sum(ct )
    from 
    (
    select trunc(InputDate) InputDate,count(*) ct from table_A group by trunc(InputDate)
    union 
    select trunc(InputDate),0 from dual
    )
    group by InputDate
      

  2.   

    select ceil(max(inputdate)-min(inputdate)) from table_A
    这个集合的结果都是日期型数据吗?
      

  3.   

    我现在是用循环实现的,不过速度太慢了,查找三十天的话要十多秒钟!
    对CodeMagic(写错了吧), shawnzhao你们的答案太高深了,我实在是看不懂,您二位是否能给个解释?谢谢
      

  4.   

    select trunc(inputdate,'dd') dat,count(*) num from table_A 
        group by trunc(inputdate,'dd')
      
    //取出有记录的那些天及该天的记录数,并按天分组  select trunc
      (
        (
          select min(trunc(inputdate,'dd')) from table_A
        )
        ,'dd'
      )+rownum-1 ,0 from all_tables 
      where rownum<
      (
        select ceil(max(inputdate)-min(inputdate)) from table_A
      )+1
      
    //利用all_tables表来构造一个记录集,
    //这个记录集的记录数是表table_A中所有的天数,每天的记录数为0  select trunc
      (
        (
          select min(trunc(inputdate,'dd')) from table_A
        )
        ,'dd'
      )+rownum-1 ,0 from all_tables //用这条语句求出所有天数,
    //用select min(trunc(inputdate,'dd')) from table_A求出起始日期
    //用all_tables表的rownum来推进每一天 
      where rownum<
      (
        select ceil(max(inputdate)-min(inputdate)) from table_A
      )+1//天数的个数由上面的语句来确定//最后对上面的两个大记录集进行分组求和,得到每一天的记录数
      

  5.   

    select trunc(InputDate),count(*) 
    from table_A 
    group by trunc(InputDate)
    order by trunc(InputDate)这是对的
    最简单的方法就是在 程序 中处理
    只是提一个建议
      

  6.   

    shawnzhao() 同学的解答有误,
    select trunc(InputDate),0 from dual
    这里第一inputdate并非dual的列,该语句不能执行。
    第二即使将dual换成table_a也不行,那样还是没有解决表中不存在的日期。CodeMagic(写错了吧) 同学的解答让我很受启发。不过我有点疑问。
    select ceil(max(inputdate)-min(inputdate)) from table_A
    这句应该是判断天数的,但是如果我的inputdate是从2004-08-05开始到2004-08-23结束
    那么语句所选出的记录是从2004-08-05到2004-08-23的,5日之前和23日之后的记录还是
    没有。我对该解答稍做修改,不过还是得要求每个月的1日必须存在于表中。
    select dat,sum(num) from
    (
      select trunc(inputdate,'dd') dat,count(*) num from table_A
      group by trunc(inputdate,'dd')
        union
      select trunc
      (
        (
          select min(trunc(inputdate,'dd')) from table_A
        )
        ,'dd'
      )+rownum-1 ,0 from all_tables
      where rownum-1<
      (
        select ceil(max(last_day(inputdate))-min(trunc(inputdate,'mm'))) from table_A
      )
    )
    group by dat
    order by dat
      

  7.   

    如果可以加表,最好是加个表,记录每天的日期,然后用它和table_A进行外连接
      

  8.   

    再试试这个:
    select dat,sum(num) from
    (
      select trunc(inputdate,'dd') dat,count(*) num from table_A
      group by trunc(inputdate,'dd')
        union
      select trunc
      (
        (
          select min(trunc(inputdate,'mm')) from table_A
        )
        ,'dd'
      )+rownum-1 ,0 from all_tables
      where rownum-2<
      (
        select ceil(max(last_day(inputdate))-min(trunc(inputdate,'mm'))) from table_A
      )
    )
    group by dat
    order by dat