表一:
name |
type |
A1 |
02 |
A1 |
03 |
A2 |
02 |
A3 |
03 |
表二:
name |
type02 |
type03 |
A1 |
是 |
是 |
A2 |
是 |
否 |
A3 |
否
|
是 |
将表一的结果转为表二有下面这几种方法。
--第一种方法
select t.code,
t.name,
decode(p1.type_code, '02', '是', '否') as 指标单位,
decode(p1.type_code, '03', '是', '否') as 决算单位
from base_unit t
left join base_unit_type p1 on t.guid = p1.unit_guid
and p1.type_code = '02'
left join base_unit_type p2 on t.guid = p2.unit_guid
and p2.type_code = '03'
where (p1.type_code is not null or p2.type_code is not null)
order by p1.type_code, p2.type_code, t.code;
--第二:
select * from (
select code, name, nvl((select '是' from base_unit_type a where a.unit_guid = t.guid
and a.type_code = '02'), '否') as 指标单位,
nvl((select '是' from base_unit_type a where a.unit_guid = t.guid
and a.type_code = '03'), '否') as 决算单位
from base_unit t
where exists (select 1s
from base_unit_type p
where p.unit_guid = t.guid
and p.type_code in ('02', '03')) ) m
order by m.指标单位 desc , m.决算单位 desc, m.code ;
--第三:
select t.code, t.name, max(case when t.type_code='02' then '是' else '否' end) as aa,
max(case when t.type_code='03' then '是' else '否' end) as bb
from
(
select t1.code, t1.name, t2.type_code
from base_unit t1, base_unit_type t2
where t1.guid = t2.unit_guid
and t2.type_code in ('02', '03')
) t
group by t.code,t.name
order by aa desc ,bb desc
--第四:
select m.code,m.name, decode(aa,1,'index_unit','NO'),decode(bb,1,'unit','NO') from (
select k.code,k.name, max(index_unit) as aa,max(unit) as bb from (
select code, name, 1 as index_unit, 0 as unit
from base_unit t
where exists (select 1
from base_unit_type p
where p.unit_guid = t.guid
and p.type_code = '02')
union all
select code, name, 0 as index_unit, 1 as unit
from base_unit t
where exists (select 1
from base_unit_type p
where p.unit_guid = t.guid
and p.type_code = '03')
) k
group by k.code, k.name
) m
order by m.aa+m.bb desc, m.aa, m.code
分享到:
相关推荐
T100制造常用查询报表V3.0.xlsx
es常用查询语法
C常用查询手册
SQL常用查询语句大全.特别适合入门学习之用
SQL经典查询语句,sql查询语句,sql常用查询语句 用SQL建表,建库,建试图···等等 sql基本查询,高级查询,牛逼查询 等等···只有你想不到的,没有你不想要的···
数据库经典常用查询语句,对于出入们的新手 有用 ! 数据库经典常用查询语句
c++常用查询手册 c++程序设计中,手头必备的查询工具之一。 头文件查询: 关键字查询: 常用ASCII码查询:
压力容器常用查询大全
Hibernate常用查询语句.doc
radware负载均衡常用查询命令
mysql常用查询语句
MySQL常用查询状态命令
oracle常用查询 问题+答案 觉得是初学 比较好的东西。
C++常用查询手册,C++程序员必备。包括头文件和关键字查询。PDF高清格式!
SQL常用查询语句.pdf
c语言库函数(c语言常用查询手册),头文件查询、#include、math.h\time.h\local.h\signal.h
web开发好帮手jquery常用查询总结,你绝对值得拥有
MySql数据数据库,常用查询命令操作大全。!!!!!!!!!!!
h3c常用查询命令.pdf
#资源达人分享计划# 财务会计常用查询用表格