继续时间分组统计,求合计的问题,在线等!

zfwyyz 2009-03-29 11:10:12
我有如下一个表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开头的单位的项目统计
...全文
190 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
zfwyyz 2009-04-02
  • 打赏
  • 举报
回复
我有如下一个表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

如果单独求合计这一行,SQL的代码怎么写
fosjos 2009-04-01
  • 打赏
  • 举报
回复
把时间限制加在这里有没有试过:
case when ... and trunc(rq,'y')=trunc(sysdate,'y') then 1 else 0
zfwyyz 2009-04-01
  • 打赏
  • 举报
回复
现在是最下面的合计不知道怎么写了
zfwyyz 2009-04-01
  • 打赏
  • 举报
回复
这个没有试过,我是菜鸟哦,case when 还不知道怎么用呢
现在关键是合计怎么计算,不知道怎么写了
zfwyyz 2009-03-31
  • 打赏
  • 举报
回复
有没有人帮忙看下该怎么写呢
fosjos 2009-03-31
  • 打赏
  • 举报
回复
那就简单些:
case when ... and trunc(rq,'y')=trunc(sysdate,'y') then 1 else 0
zfwyyz 2009-03-30
  • 打赏
  • 举报
回复
左关联怎么做呢,还有合计该怎么写

oracle里面,用 “select
nvl(dw,'合计') dw,”好像报错的
zfwyyz 2009-03-30
  • 打赏
  • 举报
回复
感觉还有点问题
我有如下一个表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在今年没有数据,我又想他在表上有名称显示,按照上面的查询好象显示不出的,不知道如何解决
fosjos 2009-03-30
  • 打赏
  • 举报
回复
(select distinct dw from table)
然后再和原来的结果做左关联
zfwyyz 2009-03-29
  • 打赏
  • 举报
回复
明天早上再向你请教
zfwyyz 2009-03-29
  • 打赏
  • 举报
回复
恩,我先试下哦,多谢了
dawugui 2009-03-29
  • 打赏
  • 举报
回复
还有我想指定增加 
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) || '单位'

dawugui 2009-03-29
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 zfwyyz 的回复:]
增加“全部”列,“合计”行
好像上面的代码不行哦
[/Quote]
看错.

这样呢?

select 
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) || '单位'

zfwyyz 2009-03-29
  • 打赏
  • 举报
回复
还有我想指定增加
c开头的单位和d开头的单位
不想增加其他字母开头的单位统计
zfwyyz 2009-03-29
  • 打赏
  • 举报
回复
我想实现如下结果
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开头的单位的项目统计


增加“全部”列,“合计”行
好像上面的代码不行哦
dawugui 2009-03-29
  • 打赏
  • 举报
回复
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
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) || '单位'
zfwyyz 2009-03-29
  • 打赏
  • 举报
回复
上次开贴,已解决问题代码
我有如下一个表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

17,377

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧