ORACLE数据库:表A 部门表 :字段:dept-id(部门ID) pid(上级单位ID) dept-name(部门名

问题描述:

ORACLE数据库:表A 部门表 :字段:dept_id(部门ID) pid(上级单位ID) dept_name(部门名称) dept_type
ORACLE数据库:
表A 部门表 :字段:dept_id(部门ID) pid(上级单位ID) dept_name(部门名称) dept_type(部门类型 0:部门,1:单位,就是上级单位)
表B 人员表:字段:user_id dept_id(所属部门ID)
表C 住房表:字段:building_id user_id lh_id(楼号ID)
表D 楼号表:字段:lh_id lh_type(楼的类型a 0:老城,1:新城) lh_kind(楼的类型B 0:多层,1:高层)
需要的结果:
单位 部门 新城多层 新城高层 老城多层 老城高层 小计
单位A 部门A 5 10 40 20 75
单位B.
1个回答 分类:综合 2014-10-04

问题解答:

我来补答
select t11.organ_name "单位",
t11.dept_name "部门",
count(case when t14.lh_type=1 and t14.lh_kind=0 then 1 else null end) "新城多层",
count(case when t14.lh_type=1 and t14.lh_kind=1 then 1 else null end) "新城高层",
count(case when t14.lh_type=0 and t14.lh_kind=0 then 1 else null end) "老城多层",
count(case when t14.lh_type=0 and t14.lh_kind=1 then 1 else null end) "老城高层",
count(1) "小计"
from (select t1.dept_name organ_name,t2.dept_name,t2.dept_id
from (select dept_id,dept_name from a where dept_type = 1) t1,
(select pid,dept_id,dept_name
from A
where dept_type = 0) t2
where t1.dept_id = t2.pid) t11,
B t12,
C t13,
D t14
where t11.dept_id = t12.dept_id
and t12.user_id=t13.user_id
and t13.lh_id=t14.lh_id
group by t11.organ_name,
t11.dept_name
 
 
展开全文阅读
剩余:2000
也许感兴趣的知识