表结构:
出生日期 性别
2006-07-01 女
2006-07-04 男
2006-07-06 男
2005-07-01 男
2005-07-01 女
2007-07-01 男
2007-07-01 女
2007-07-01 女
-------
得到下面的结构:
年份 总男 总女 出生男 出生女
2005 1 1 1 1
2006 3 2 2 1
2007 4 4 1 2注释:
2006年的总男=2006年出生男+2005年的总男
出生日期 性别
2006-07-01 女
2006-07-04 男
2006-07-06 男
2005-07-01 男
2005-07-01 女
2007-07-01 男
2007-07-01 女
2007-07-01 女
-------
得到下面的结构:
年份 总男 总女 出生男 出生女
2005 1 1 1 1
2006 3 2 2 1
2007 4 4 1 2注释:
2006年的总男=2006年出生男+2005年的总男
解决方案 »
- 冷备份的完整例子
- “企业版本40000美元每CPU,或者800美元每用户”是什么意思?
- 这个SQL特别慢,大家帮我看看吧
- 高手请进,关于用OEM备份数据库,急啊
- 游标记录数量太大,时间太长,造成ORA-01555错误,怎么解决?
- AIX4.3的/usr目录下,哪些是可删除的无用文件?
- oracle9i 的init文件为什么是 init.ora+日期 ?
- 初学Oracle,请教各位大虾。
- Oracle11g透明网关连SQL Server出现“ORA-28545: 连接代理时 Net8 诊断到错误..........”
- 注册商标符号(特殊符号),oracle存储时变为乱码,oracle字符集为ZHS16GBK;类似情况如何处理?
- constant record 类型的初始化问题
- [求助]实现一条查询语句
1. 自动汇总函数rollup,cube,
2. rank 函数, rank,dense_rank,row_number
3. lag,lead函数
4. sum,avg,的移动增加,移动平均数
5. ratio_to_report报表处理函数
6. first,last取基数的分析函数
---------- --
2004-02-04 女
2004-07-03 女
2006-03-05 女
2007-05-06 女
2007-08-03 男
2007-01-03 男
2007-10-25 男
2004-01-19 男已选择8行。SQL> select a.y,sum(nvl(b.s1,0)) over(order by a.y) t1,sum(nvl(b.s2,0)) over(order by a.y) t2, nvl(b.s1,0) s1,nvl(b.s2,0) s2 from (select to_char(2000+rownum) y from t connect by rownum<9) a, (select to_char(bday,'yyyy') y,sum(decode(s,'男',1,0)) s1 ,sum(decode(s,'男',0,1)) s2 from t group by to_char(bday,'yyyy')) b where a.y=b.y(+);Y T1 T2 S1 S2
----- ---- ---- ---- ----
2001 0 0 0 0
2002 0 0 0 0
2003 0 0 0 0
2004 1 2 1 2
2005 1 2 0 0
2006 1 3 0 1
2007 4 4 3 1
2008 4 4 0 0已选择8行。
(select count(*) from 表 where 出生日期<=a.year||'-12-31' and 性别='男' ) as 总男,
(select count(*) from 表 where 出生日期<=a.year||'-12-31' and 性别='女' ) as 总女,
(select count(*) from 表 where 出生日期<=a.year||'-12-31' and 出生日期>=a.year||'-01--1' and 性别='男' ) as 出生男,
(select count(*) from 表 where 出生日期<=a.year||'-12-31' and 出生日期>=a.year||'-01--1' and 性别='女' ) as 出生女 from (select distinct strsub(to_char(出生日期,'YYYY-MM-DD'),1,4) year from 表 ) a