寻求一统计分组SQL语句,大家进来研究一下

lovezky 2007-07-03 11:45:45

现有数据表如下:
id insertdate content
1 20070601 eee
2 20070601 aaa
3 20070605 bbb
4 20070607 ttt
5 20070607 aaa
6 20070611 aaa
7 20070611 aaa
8 20070505 bbb
9 20070506 ttt
10 20070506 aaa
11 20070521 aaa
12 20070521 aaa

希望得到结果如下:

日期 200705(条数) 200706(条数)
1 0 2
2 0 0
3 0 0
4 0 0
5 1 1
6 2 0
7 0 2
8 0 0
9 0 0
10 0 0
11 0 2
。。。。。。。
21 2 0
22 0 0
。。。。。。。
31 0 0

请大家看看
...全文
198 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
lovezky 2007-07-06
  • 打赏
  • 举报
回复
多谢,但是感觉还是不是很符合,结了再开
1980czd 2007-07-04
  • 打赏
  • 举报
回复
select b.num,nvl(a.y200705,0),nvl(a.y200706,0) from
(
select to_number(substr(insertdate, 7, 2)) rq,
sum(decode(substr(insertdate, 1, 6), 200705, 1, 0)) y200705,
sum(decode(substr(insertdate, 1, 6), 200706, 1, 0)) y200706
from czd_t01
group by substr(insertdate, 7, 2)
) a,

(select rownum num from dual connect by rownum<32) b

where b.num=a.rq(+)

上面的看错条件了
1980czd 2007-07-04
  • 打赏
  • 举报
回复
select b.num,nvl(a.y2006,0),nvl(a.y2007,0) from
(
select to_number(substr(insertdate, 7, 2)) rq,
sum(decode(substr(insertdate, 1, 4), 2006, 1, 0)) y2006,
sum(decode(substr(insertdate, 1, 4), 2007, 1, 0)) y2007
from czd_t01
group by substr(insertdate, 7, 2)
) a,

(select rownum num from dual connect by rownum<32) b

where b.num=a.rq(+)
lovezky 2007-07-04
  • 打赏
  • 举报
回复
结果是按日期来排序的,没有的日期就填0,楼上的好像不太符合
lovezky 2007-07-03
  • 打赏
  • 举报
回复
沙发
1982dragon 2007-07-03
  • 打赏
  • 举报
回复
select substr(insertdate,7,2) 日期,
count(select *
from table
where to_char(insertdate,'YYYYMMDD')='200705') 200705(条数),
count(select *
from table
where to_char(insertdate,'YYYYMMDD')='200706') 200706(条数)
from table
group by 日期

17,377

社区成员

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

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