SELECT A.*,INTRUST_PLAN.c_cprcompany_type,INTRUST_PLAN.c_pbccode,INTRUST_PLAN.c_pbcasset_code,INTRUST_PLAN.c_pbcasset_name,INTRUST_PLAN.c_invest_object ,STANDERD_DATA.c_pool_code FROM (select financ.fld_type,
financ.fld_code,
financ_info.fld_financ_name,
financ.fld_asset_type,
financ.fld_asset_code,
financ.fld_asset_name,
sum(financ.fld_amount) amount
from tb_financ_investassetno financ,
tb_financing_info financ_info
where financ.fld_code = financ_info.fld_financ_code
and financ.fld_queue = '2'
and financ.fld_status = '1'
and financ.fld_transdate <= '20130717'
group by financ.fld_type,
financ.fld_code,
financ.fld_asset_type,
financ.fld_asset_code,
financ.fld_asset_name,
financ_info.fld_financ_name
union
select abundle.fld_type,
abundle.fld_code abundle_fld_code,
abundle_info.fld_assetbundle_name abundle_fld_name,
abundle.fld_asset_type,
abundle.fld_asset_code,
abundle.fld_asset_name,
sum(abundle.fld_amount) amount
from tb_abundle_investassetno abundle,
tb_assetbundle_info abundle_info
where abundle.fld_code = abundle_info.fld_assetbundle_code
and abundle.fld_queue = '2'
and abundle.fld_status = '1'
and abundle.fld_transdate <= '20130717'
group by abundle.fld_type,
abundle.fld_code,
abundle.fld_asset_type,
abundle.fld_asset_code,
abundle.fld_asset_name,
abundle_info.fld_assetbundle_name) A left join atintrust_plan INTRUST_PLAN
on A.fld_asset_code=INTRUST_PLAN.C_PRD_CODE left join atstanderd_data STANDERD_DATA
on STANDERD_DATA.C_PROD_CODE=INTRUST_PLAN.c_prd_code
------------------------------------------------------------------------------------------------------------------------
Oracle中的Union、Union All、Intersect、Minus
众所周知的几个结果集集合操作命令,今天详细地测试了一下,发现一些问题,记录备考。
假设我们有一个表Student,包括以下字段与数据:
drop table student;
create table student
(
id int primary key,
name nvarchar2(50) not null,
score number not null
);
insert into student values(1,'Aaron',78);
insert into student values(2,'Bill',76);
insert into student values(3,'Cindy',89);
insert into student values(4,'Damon',90);
insert into student values(5,'Ella',73);
insert into student values(6,'Frado',61);
insert into student values(7,'Gill',99);
insert into student values(8,'Hellen',56);
insert into student values(9,'Ivan',93);
insert into student values(10,'Jay',90);
commit;
Union和Union All的区别。
select *
from student
where id < 4
union
select *
from student
where id > 2 and id < 6
结果将是
1 Aaron 78
2 Bill 76
3 Cindy 89
4 Damon 90
5 Ella 73
如果换成Union All连接两个结果集,则返回结果是:
1 Aaron 78
2 Bill 76
3 Cindy 89
3 Cindy 89
4 Damon 90
5 Ella 73
可以看到,Union和Union All的区别之一在于对重复结果的处理。
接下来我们将两个子查询的顺序调整一下,改为
--Union
select *
from student
where id > 2 and id < 6
union
select *
from student
where id < 4
看看执行结果是否和你期望的一致?
--Union All
select *
from student
where id > 2 and id < 6
union all
select *
from student
where id < 4
那么这个呢?
据此我们可知,区别之二在于对排序的处理。Union All将按照关联的次序组织数据,而Union将进行依据一定规则进行排序。那么这个规则是?我们换个查询方式看看:
select score,id,name
from student
where id > 2 and id < 6
union
select score,id,name
from student
where id < 4
结果如下:
73 5 Ella
76 2 Bill
78 1 Aaron
89 3 Cindy
90 4 Damon
和我们预料的一致:将会按照字段的顺序进行排序。之前我们的查询是基于id,name,score的字段顺序,那么结果集将按照id优先进行排序;而现在新的字段顺序也改变了查询结果的排序。并且,是按照给定字段a,b,c...的顺序进行的order by。即结果是order by a,b,c...........的。我们看下一个查询:
select score,id,name
from student
where id > 2
union
select score,id,name
from student
where id < 4
结果如下:
56 8 Hellen
61 6 Frado
73 5 Ella
76 2 Bill
78 1 Aaron
89 3 Cindy
90 4 Damon
90 10 Jay
93 9 Ivan
99 7 Gill
可以看到,对于score相同的记录,将按照下一个字段id进行排序。如果我们想自行控制排序,是不是用order by指定就可以了呢?答案是肯定的,不过在写法上有需要注意的地方:
select score,id,name
from student
where id > 2 and id < 7
union
select score,id,name
from student
where id < 4
union
select score,id,name
from student
where id > 8
order by id desc
order by子句必须写在最后一个结果集里,并且其排序规则将改变操作后的排序结果。对于Union、Union All、Intersect、Minus都有效。
=================================================================================================================
Intersect和Minus的操作和Union基本一致,这里一起总结一下:
Union,对两个结果集进行并集操作,不包括重复行,同时进行默认规则的排序;
Union All,对两个结果集进行并集操作,包括重复行,不进行排序;
Intersect,对两个结果集进行交集操作,不包括重复行,同时进行默认规则的排序;
Minus,对两个结果集进行差操作,不包括重复行,同时进行默认规则的排序。
可以在最后一个结果集中指定Order by子句改变排序方式。
----------------------------------------------------------------------------------------------------------------------------
Union与Union All的区别如果我们需要将两个select语句的结果作为一个整体显示出来,我们就需要用到union或者union all关键字。union(或称为联合)的作用是将多个结果合并在一起显示出来。 union和union all的区别是,union会自动压缩多个结果集合中的重复结果,而union all则将所有的结果全部显示出来,不管是不是重复。 Union:对两个结果集进行并集操作,不包括重复行,同时进行默认规则的排序; Union All:对两个结果集进行并集操作,包括重复行,不进行排序; select empno,ename from emp union select deptno,dname from dept 我们没有必要在每一个select结果集中使用order by子句来进行排序,我们可以在最后使用一条order by来对整个结果进行排序。例如: select empno,ename from emp union select deptno,dname from dept order by ename;
-------------------------------------------------------------------------hundsun----------------------------------------
--在数据库中利用cast进行数据转换
select distinct cast(fld_asset_type as int) fld_asset_type,fld_asset_name from tb_asset_rule_info order by cast(fld_asset_type as int) asc
--trim进行去掉空格
select * from tsys_parameter where trim(param_code) = 'user_code'
--大于最大日期的查询
SELECT A.*
FROM ATSELF_INCOME A
WHERE A.D_DATE = (SELECT MAX(D_DATE)
FROM ATSELF_INCOME
WHERE c_selfabundcode = A.c_selfabundcode
AND c_selfabundcode = 'AP90107550')
--exits的用法
select i.*
from tb_assetbundle_info i
where 1 = 1
and exists (select c_financ_code
from atproductorg prd_org
where prd_org.c_financ_code = i.fld_assetbundle_code
and prd_org.c_type = '2'
and prd_org.c_org_id in ('0001'))
order by fld_setupdate desc
--------------------------------------------- left join 在多表中添加了where---------
select f.*, b.branch_name
from tb_financing_info f
left join tsys_branch b on f.c_org_id = b.branch_code
where 1 = 1
and FLD_FINANC_CODE = '12013F113'
and FLD_INNER_TYPE NOT IN ('1', '2')