我有如下一个表table
dw sj rq
aa 12 2009-03-27 12:00:00
ab 13 2009-03-27 11:30:00
aa 11 2009-03-27 11:05:00
ab 21 2009-03-26 16:00:00
aa sd 2009-03-26 12:00:00
ac sf 2009-03-26 11:00:00
aa 12 2009-03-25 12:00:00
aa 19 2009-03-20 12:30:00
aa 25 2009-03-01 12:00:00
aa cv 2009-01-01 12:00:00
ca er 2009-03-27 22:12:12
cb hg 2009-01-27 22:12:12
我想实现如下结果
dw 全部 当日 三日 七日 一个月
aa 7 2 4 5 6
ab 2 1 2 2 2
ac 1 0 1 1 1
合计 10 3 7 8 9
c单位 2 1 0 0 0
d单位 ……
说明下:第二列“全部”就是“aa”在数据库出现的总次数
合计是对aa,ab,ac……以a开头的数的合计(这个功能是主要想解决的)
同时下面再增加一行,以C开头的或者再增加一行以D开头的单位的项目统计
dw sj rq
aa 12 2009-03-27 12:00:00
ab 13 2009-03-27 11:30:00
aa 11 2009-03-27 11:05:00
ab 21 2009-03-26 16:00:00
aa sd 2009-03-26 12:00:00
ac sf 2009-03-26 11:00:00
aa 12 2009-03-25 12:00:00
aa 19 2009-03-20 12:30:00
aa 25 2009-03-01 12:00:00
aa cv 2009-01-01 12:00:00
ca er 2009-03-27 22:12:12
cb hg 2009-01-27 22:12:12
我想实现如下结果
dw 全部 当日 三日 七日 一个月
aa 7 2 4 5 6
ab 2 1 2 2 2
ac 1 0 1 1 1
合计 10 3 7 8 9
c单位 2 1 0 0 0
d单位 ……
说明下:第二列“全部”就是“aa”在数据库出现的总次数
合计是对aa,ab,ac……以a开头的数的合计(这个功能是主要想解决的)
同时下面再增加一行,以C开头的或者再增加一行以D开头的单位的项目统计
解决方案 »
- 初学oracle,问一个问题
- 急急急!在线求一个SQL语句 !
- oracle中load用法
- 谁有建模工具给个下载地址吧!谢谢了!:)
- 请问备用数据库可以逻辑导出吗?
- 急!救助!SQL Server转换为Oracle语句
- 关于树形分组统计的难题。。。
- 关于恢复,当我丢失了所有控制文件?
- 关于数据库的运行模式!
- 配置好Configuration Assistant后,启动OracleOraHome81ManagementServer服务出现奇怪错误!
- racle.sql.BLOB blob = (oracle.sql.BLOB) b;这里报classCastException
- 关于使用恢复目录进行备份的问题
我有如下一个表table
dw sj rq
a 12 2009-03-27 12:00:00
b 13 2009-03-27 11:30:00
a 11 2009-03-27 11:05:00
b 21 2009-03-26 16:00:00
a sd 2009-03-26 12:00:00
c sf 2009-03-26 11:00:00
a 12 2009-03-25 12:00:00
a 12 2009-03-20 12:30:00
a 12 2009-03-01 12:00:00
我想实现如下结果
dw 当日 三日 七日 一个月
a 2 4 5 6
b 1 2 2 2
c 0 1 1 1 代码为
select
dw,
sum(case when trunc(sysdate)=trunc(rq) then 1
else 0
end
) 当日,
sum(case when trunc(sysdate)-trunc(rq) <=3 then 1
else 0
end
) 三日,
sum(case when trunc(sysdate)-trunc(rq) <=7 then 1
else 0
end
) 七日,
sum(case when trunc(sysdate)-trunc(rq) <=30 then 1
else 0
end
) 一个月
from table
group by dw
dw,
sum(case when trunc(sysdate)=trunc(rq) then 1
else 0
end
) 当日,
sum(case when trunc(sysdate)-trunc(rq) <=3 then 1
else 0
end
) 三日,
sum(case when trunc(sysdate)-trunc(rq) <=7 then 1
else 0
end
) 七日,
sum(case when trunc(sysdate)-trunc(rq) <=30 then 1
else 0
end
) 一个月
from table
group by dw
union all
select
substr(dw,1,1) || '单位' dw,
sum(case when trunc(sysdate)=trunc(rq) then 1
else 0
end
) 当日,
sum(case when trunc(sysdate)-trunc(rq) <=3 then 1
else 0
end
) 三日,
sum(case when trunc(sysdate)-trunc(rq) <=7 then 1
else 0
end
) 七日,
sum(case when trunc(sysdate)-trunc(rq) <=30 then 1
else 0
end
) 一个月
from table
group by substr(dw,1,1) || '单位'
dw 全部 当日 三日 七日 一个月
aa 7 2 4 5 6
ab 2 1 2 2 2
ac 1 0 1 1 1
合计 10 3 7 8 9
c单位 2 1 0 0 0
d单位 …… 说明下:第二列“全部”就是“aa”在数据库出现的总次数
合计是对aa,ab,ac……以a开头的数的合计(这个功能是主要想解决的)
同时下面再增加一行,以C开头的或者再增加一行以D开头的单位的项目统计
增加“全部”列,“合计”行
好像上面的代码不行哦
dw,
count(1) 全部,
sum(case when trunc(sysdate)=trunc(rq) then 1
else 0
end
) 当日,
sum(case when trunc(sysdate)-trunc(rq) <=3 then 1
else 0
end
) 三日,
sum(case when trunc(sysdate)-trunc(rq) <=7 then 1
else 0
end
) 七日,
sum(case when trunc(sysdate)-trunc(rq) <=30 then 1
else 0
end
) 一个月
from table
group by dw
with rollup
union all
select
substr(dw,1,1) || '单位' dw,
count(1) 全部,
sum(case when trunc(sysdate)=trunc(rq) then 1
else 0
end
) 当日,
sum(case when trunc(sysdate)-trunc(rq) <=3 then 1
else 0
end
) 三日,
sum(case when trunc(sysdate)-trunc(rq) <=7 then 1
else 0
end
) 七日,
sum(case when trunc(sysdate)-trunc(rq) <=30 then 1
else 0
end
) 一个月
from table
group by substr(dw,1,1) || '单位'
c开头的单位和d开头的单位
不想增加其他字母开头的单位统计select
nvl(dw,'合计') dw,
count(1) 全部,
sum(case when trunc(sysdate)=trunc(rq) then 1
else 0
end
) 当日,
sum(case when trunc(sysdate)-trunc(rq) <=3 then 1
else 0
end
) 三日,
sum(case when trunc(sysdate)-trunc(rq) <=7 then 1
else 0
end
) 七日,
sum(case when trunc(sysdate)-trunc(rq) <=30 then 1
else 0
end
) 一个月
from table
group by dw
with rollup
union all
select
substr(dw,1,1) || '单位' dw,
count(1) 全部,
sum(case when trunc(sysdate)=trunc(rq) then 1
else 0
end
) 当日,
sum(case when trunc(sysdate)-trunc(rq) <=3 then 1
else 0
end
) 三日,
sum(case when trunc(sysdate)-trunc(rq) <=7 then 1
else 0
end
) 七日,
sum(case when trunc(sysdate)-trunc(rq) <=30 then 1
else 0
end
) 一个月
from table where substr(dw,1,1) in ('c' , 'd')
group by substr(dw,1,1) || '单位'
我有如下一个表table
dw sj rq
aa 12 2009-03-27 12:00:00
ab 13 2009-03-27 11:30:00
aa 11 2009-03-27 11:05:00
ab 21 2009-03-26 16:00:00
aa sd 2009-03-26 12:00:00
ac sf 2009-03-26 11:00:00
aa 12 2009-03-25 12:00:00
aa 19 2009-03-20 12:30:00
aa 25 2009-03-01 12:00:00
aa cv 2009-01-01 12:00:00
ad er 2008-03-27 22:12:12 (注意这个时间)
我想实现如下结果
dw 全部 当日 三日 七日 一个月
aa 7 2 4 5 6
ab 2 1 2 2 2
ac 1 0 1 1 1
ad 0 0 0 0 0
合计 10 3 7 8 9
如果我想统计今年以来2009-01-01的数据,但是ad在今年没有数据,我又想他在表上有名称显示,按照上面的查询好象显示不出的,不知道如何解决
然后再和原来的结果做左关联
nvl(dw,'合计') dw,”好像报错的
case when ... and trunc(rq,'y')=trunc(sysdate,'y') then 1 else 0
case when ... and trunc(rq,'y')=trunc(sysdate,'y') then 1 else 0
现在关键是合计怎么计算,不知道怎么写了
dw sj rq
aa 12 2009-03-27 12:00:00
ab 13 2009-03-27 11:30:00
aa 11 2009-03-27 11:05:00
ab 21 2009-03-26 16:00:00
aa sd 2009-03-26 12:00:00
ac sf 2009-03-26 11:00:00
aa 12 2009-03-25 12:00:00
aa 19 2009-03-20 12:30:00
aa 25 2009-03-01 12:00:00
aa cv 2009-01-01 12:00:00
ca er 2009-03-27 22:12:12
cb hg 2009-01-27 22:12:12
我想实现如下结果
dw 全部 当日 三日 七日 一个月
aa 7 2 4 5 6
ab 2 1 2 2 2
ac 1 0 1 1 1
合计 10 3 7 8 9 如果单独求合计这一行,SQL的代码怎么写