
oracle 中 select 多字段 ,但是只group by 其中某些字段
(用AVG,SUM……也是不行的取出的period_year不对) 展开
1、创建测试表,
create table exp_report_accounts(account_id varchar2(20), responsibility_cent_id varchar2(20), period_name varchar2(20));
2、插入测试数据
insert into exp_report_accounts values(1001, 9001, 20130502);
insert into exp_report_accounts values(1002, 9001, 20140902);
insert into exp_report_accounts values(1003, 9002, 20130915);
insert into exp_report_accounts values(1004, 9003, 20131212);
insert into exp_report_accounts values(1005, 9003, 20141212);
commit;
3、查询表中全量数据,select t.*, rowid from exp_report_accounts t;
4、编写sql,select多字段 ,而且做group by操作;
select account_id,
responsibility_cent_id,
substr(period_name, 1, 4) as period_year,
count(*)
from exp_report_accounts era
group by account_id, responsibility_cent_id, substr(period_name, 1, 4)
2016-05-13
select era.account_id,
era.responsibility_center_id,
sum(substr(era.period_name,0,4)) period_year
from exp_report_accounts era
group by era.account_id, era.responsibility_center_id
可以参考一下! 希望对你所以帮助
group集合计算出的结果,要不就是group要素自己,要不就是经过统计计算的结果.
select a from table group b,肯定不行, 但是select max(a) from table group b就可以
你这个问题就是需要按年统计,自然是在group中动手
group by 加上 substr(period_name) 就能select出来 substr(period_name)
select aa.account_id,aa.responisibity_center_id,period_year
from (select t.*,substr(t.period_name,0,4) period_year
from exp_report_accounts t)aa
group by aa.account_id,aa.responisibity_center_id,period_year