现有两个表
表A
上几部门代号 科室代码
1 4
1 5
2 6
2 7
3 8
表B
代号 名称
1 部门1
2 部门2
3 部门3
4 科室1
5 科室2
6 科室1
7 科室3
8 科室1要查询出结果
代号 名称
1 部门1
4 部门1科室1
5 部门1科室2
2 部门2
6 部门2科室1
7 部门2科室2
3 部门3
8 部门3科室1这样的查询如何写?
表A
上几部门代号 科室代码
1 4
1 5
2 6
2 7
3 8
表B
代号 名称
1 部门1
2 部门2
3 部门3
4 科室1
5 科室2
6 科室1
7 科室3
8 科室1要查询出结果
代号 名称
1 部门1
4 部门1科室1
5 部门1科室2
2 部门2
6 部门2科室1
7 部门2科室2
3 部门3
8 部门3科室1这样的查询如何写?
解决方案 »
- conn / as sysdba与OracleDBConsoleorcl的冲突
- 请问如果将Windows下的Oracle迁移到Linux下需要注意什么?
- oracle10g的口令改变
- 求助SQL问题
- 请问这个日期格式转换如何实现!
- sql下有xp_cmdshell,Oracle下有相对应的是啥啊?
- 请教高手 oracle中的sql语句如何改写成sqlserver中sql
- ora8.1.7支持windows xp pro sp1吗?
- 请教如何根据txt来更新数据库表里的部分字段?
- oracle clob 字段问题
- Oracle里的procedure怎么样返回记录集?
- 请高手解释下oracle的自带过程Submit()
from 表A a,
表B b,
表B c
where b.bmid=a.bmid
and a.ksid=c.bmid
这样只能把含下级部门的查询出来代号 名称4 部门1科室1
5 部门1科室26 部门2科室1
7 部门2科室28 部门3科室1
where b.code=c.code) from
2 (select '' as id, code as code from b where instr(name, '部门') > 0
3 union all
4 select id, code from a
5 )c
6 connect by prior code = id start with id is null or id = ''
7 ;C (SELECTNAM
- ----------
1 部门1
4 部门1科室1
5 部门1科室2
2 部门2
6 部门2科室1
7 部门2科室3
3 部门3
8 部门3科室1已选择8行。SQL>
where a.dept_id=b.did(+) and b.id=c.dept_id(+)
order by deptname
name
form
(select distinct a.code1 code,b.name name from a,b where a.code1=b.code) c
union
select
c.name || d.name
from
(select distinct a.code1 code,b.name name from a,b where a.code1=b.code) c,
(select distinct a.code2 code,b.name name from a,b where a.code2=b.code) d
where
a.code1=c.code
and a.code2=d.code
思路:分别找出部门号+部门名;科室号+科室名。组建两个子标,然后,分成只有[部门名],和[部门名科室名]两个子集。
SELECT ID,DECODE(ID, CODE, '', FIRST_VALUE(NAME) OVER (PARTITION BY CODE ORDER BY CODE DESC)) || NAME
FROM (
SELECT B.*, NVL(A.ID, B.ID) AS CODE
FROM B
LEFT JOIN A ON BB.ID = A.CODE
)