蓝红石

生活与工作点滴

   :: 首页  ::  :: 联系 :: 订阅 订阅  :: 管理

 

解决方案:

create table t_20120512_a (
id varchar2(6) primary key,
cost number(3),
p2 number(3),
p3 number(3)
)
/
create table t_20120512_b
(
id varchar2(6) primary key,
name varchar2(50),
b varchar2(100)
)
/
insert into t_20120512_a values ('100',1,1,1);
insert into t_20120512_a values ('101001',2,2,2);
insert into t_20120512_a values ('101002',3,3,3);
commit;
insert into t_20120512_b values ('100','语文',null);
insert into t_20120512_b values ('101','物理',null);
insert into t_20120512_b values ('101001','电学',null);
insert into t_20120512_b values ('101002','力学',null);
commit;

select a.id, b.name ,a.total from
(
select distinct case when GROUPING(substr(id,1,3))=0 then substr(id,1,3) else id end id,sum(cost) total from t_20120512_a group by grouping SETS (id,substr(id,1,3))
) a,
t_20120512_b b 
where a.id=b.id;

 

posted on 2012-05-12 22:51  蓝红石  阅读(448)  评论(0编辑  收藏  举报