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
(
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
解决方案 »
- Oracle 64位客户端连接PLSQL的问题,求大神
- 创建数据库的时候出现错误,启动 Database Control 时出错,求解
- redhat5.2安装oracle10g因SID问题(十万火急在线等)
- aix 下用G++编译ORACLE9I下的OCI程序。(AIX 5。3)安装ORACLE过程没出一点错误。
- 进入SQLPlus WorkSheet的服务老出错????
- 刚到Oracle版本.准备天天送分:顺便学习,第四个问题:SQL 中的"sp_executesql" Oracle 是怎么写的呢??
- 8.0.5的数据如何导入到9i中
- >>>>>用toad进行导出时遇到的问题
- odbc中的varchar2(2000)问题。
- P4的计算机上为何无法安装Oracle8,操作系统为Windows 2000 Server
- 安装oracle9i时遇到的问题-----hostdef扩展名不存在?
- oracle 9i 简体转繁体 等待中
加一条为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
这个集合的结果都是日期型数据吗?
对CodeMagic(写错了吧), shawnzhao你们的答案太高深了,我实在是看不懂,您二位是否能给个解释?谢谢
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//天数的个数由上面的语句来确定//最后对上面的两个大记录集进行分组求和,得到每一天的记录数
from table_A
group by trunc(InputDate)
order by trunc(InputDate)这是对的
最简单的方法就是在 程序 中处理
只是提一个建议
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
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