Oracle表的常用查询实验(八)

Oracle表的常用查询实验(八)

1.问题描述:

有一store_fee表,表中有四个字段(会员卡编号、办卡店编号、消费情况、消费店编号)

Oracle表的常用查询实验(八)

现要统计各店的办卡总计和消费总计

2.需求分析:

在A店办卡的会员,可能会在其他店里进行消费

3.解答过程:

(1)求各店的办卡统计情况

Oracle表的常用查询实验(八)

(2)求各店的消费统计情况

Oracle表的常用查询实验(八)

(3)将以上2个结果集联合起来

Oracle表的常用查询实验(八)

4.SQL代码:

select t1.dept_no, t1.办卡统计, t2.消费统计
from (select dept_no, count(*) 办卡统计 from store_fee group by dept_no) t1
left join (select deptno_no2, sum(fee) 消费统计
from store_fee
group by deptno_no2) t2
on t1.dept_no = t2.deptno_no2
order by dept_no;