本随笔文章,由个人博客(鸟不拉屎)转移至博客园

发布时间: 2018 年 11 月 29 日

原地址:https://niaobulashi.com/archives/oracle-select-all.html


刚刚开发需求写了个SQL,记个笔记,学习下关于数据库的多表合并查询的用法


  select t.* from A t
UNION ALL/UNION/Intersect/MINUS
select s.* from B s;

UNION ALL

使用UNION ALL,表示取A、B的合集,不过滤重复的数据行

UNION

使用UNION,会将结果集A和结果集B进行UNION ALL运算,然后取两者交集的余集作为结果集

Intersect

使用Intersect,会将结果集A和结果集B进行UNION ALL运算,然后两者之间的集交集作为结果集和UNION刚好相反

MINUS

使用MINUS,取结果集A减去结果集B留下的差集,注:如果结果集A小于等于结果集B,返回空结果集.


好啦,下面进入实战阶段,我就直接将我写的SQL贴出来吧

select a.*
from (select t.c_fund_account_name as "fundAccountNo", --基金账号
tfp.project_code as "projectCode", --项目编号
tfp.project_name as "projectName", --项目名称
tfp.project_shortname as "projectShortName", --项目简称
c.c_fund_name as "fundName", --基金产品名称
c.c_fund_code as "fundCode", --基金产品代码
nvl(thold.subsistAssetsShare, 0) as "subsisAssetsShare", --份额(家族)
to_char(thold.updateDate, 'yyyy-MM-dd') as "updateDate", --日期
nvl(c.c_current_share, 0) as "currentShare", --份额(基金网站)
to_char(c.d_date, 'yyyy-MM-dd') as "dateDate", --日期
(nvl(thold.subsistAssetsShare, 0) - nvl(c.c_current_share, 0)) as "diffValue", --差值
CAST((CASE
WHEN (nvl(thold.subsistAssetsShare, 0) -
nvl(c.c_current_share, 0)) = 0 THEN
'1'
WHEN (nvl(thold.subsistAssetsShare, 0) -
nvl(c.c_current_share, 0)) <> 0 THEN
'0'
END) as nvarchar2(2)) as "identical" --是否一致
from t_fund_account t
inner join (select fhs.*,
row_number() over(partition by fhs.c_fund_account_no, fhs.c_project_code order by fhs.d_date desc) rn
from td_fund_holding_share fhs) c
on t.c_fund_account_name = c.c_fund_account_no
and t.c_project_code = c.c_project_code
LEFT JOIN (SELECT tha.project_code as projectCode,
sum(tha.current_share) as subsistAssetsShare, -- 持有份额
sum(tha.current_cost) as currentCost, --当前成本
sum(tha.accumulated_profit) as accumulatedProfit, --累积利润
max(tha.update_time) as updateDate
FROM t_hold_assets tha
left join t_polling_product p
on tha.c_product_code = p.c_product_code
WHERE 1 = 1
and tha.delete_flag = '0'
and p.c_stock_type_level1 = '0'
and p.c_stock_type_level2 = '01'
GROUP BY tha.project_code) thold
on thold.projectCode = t.c_project_code
left join t_family_project tfp
on tfp.project_code = t.c_project_code
and tfp.delete_flag = '0'
where rn = 1
AND t.c_fund_account_type = '1' --基金账户
AND t.delete_flag = '0' UNION ALL select t.c_fund_account_name as "fundAccountNo", --基金账号
tfp.project_code as "projectCode", --项目编号
tfp.project_name as "projectName", --项目名称
tfp.project_shortname as "projectShortName", --项目简称
CAST('' as nvarchar2(50)) as "fundName", --基金产品名称
CAST('' as nvarchar2(50)) as "fundCode", --基金产品代码
nvl(thold.subsistAssetsShare, 0) as "subsisAssetsShare", --份额(家族)
to_char(thold.updateDate, 'yyyy-MM-dd') as "updateDate", --日期
to_number(nvl('', 0)) as "currentShare", --份额(基金网站)
to_char(CAST('' as nvarchar2(50)), 'yyyy-MM-dd') as "dateDate", --日期
nvl(thold.subsistAssetsShare, 0) - nvl('', 0) as "diffValue", --差值
CAST((CASE
WHEN (nvl(thold.subsistAssetsShare, 0) - nvl('', 0)) = 0 THEN
'1'
WHEN (nvl(thold.subsistAssetsShare, 0) - nvl('', 0)) <> 0 THEN
'0'
END) as nvarchar2(2)) as "identical" --是否一致
from t_fund_account t
LEFT JOIN (SELECT tha.project_code as projectCode,
sum(tha.current_share) as subsistAssetsShare, -- 持有份额
max(tha.update_time) as updateDate
FROM t_hold_assets tha
left join t_polling_product p
on tha.c_product_code = p.c_product_code
WHERE 1 = 1
and tha.delete_flag = '0'
and p.c_stock_type_level1 = '0'
and p.c_stock_type_level2 = '01'
GROUP BY tha.project_code) thold
on thold.projectCode = t.c_project_code
left join t_family_project tfp
on t.c_project_code = tfp.project_code
and tfp.delete_flag = '0'
where t.c_fund_account_name not in
(select td.c_fund_account_no from td_fund_holding_share td)
and t.c_fund_account_type = '1'
and t.delete_flag = '0') a
order by a."diffValue" desc, a."projectCode" desc

上面sql具体意思是:查询出基金信息,A和基金对比先查询其中有的数据,再union all A有基金没有的数据,一起取个并集。OK,需求完成

哈哈哈哈,其实只要你SQL写得牛逼,然后了解下业务流程,什么都好说哈哈哈

哦对了,最后啰嗦一句。 对于这些并集计算之后,需要排序 则语法为:

select t.* from (语句1 union all 语句2)

t order by t.id;

oracle的多表合并查询-工作心得的更多相关文章

  1. SQL多表合并查询结果

    两表合并查询,并同时展示及分页SELECT a.* FROM ( ( SELECT punycode, `domain`, 'Success' AS state, add_time, AS refun ...

  2. Oracle 表复杂查询之多表合并查询

    转自:https://www.cnblogs.com/GreenLeaves/p/6635887.html 本文使用到的是oracle数据库scott方案所带的表,scott是oracle数据库自带的 ...

  3. mysql——多表——合并查询结果

    合并查询结果 合并查询结果 是将多个select语句的查询结果合并到一起 union关键字,数据库会将所有的查询结果合并到一起,然后除掉相同的记录: union all关键字,只是简单的合并到一起 前 ...

  4. ORACLE数据库多表关联查询效率问题解决方案

    最近在做项目中遇到多表关联查询排序的效率问题(5张以上40W+数据的表),查询一次大概要20多秒,经过一番苦思冥想,处理方案如下: 1.软件设计初期,需要一对一关联的表应该设计在一张大表里,这样虽然字 ...

  5. 一条sql,有分页,表合并查询,多表连接,用于oracle数据库

    SELECT * FROM ( SELECT TT.*,ROWNUM RN FROM ( SELECT A.CASE_ID AS TREATID, A.TYPE AS TYPE, B.CONTENT ...

  6. ORACLE EBS常用表及查询语句(最终整理版)

    建议去看参考二 参考一:                                                              call fnd_global.APPS_INITI ...

  7. SQL 两张结构一样的表合并查询 .

    select * from table1 union all select * from table2 union all 是所有的都显示出来: select * from table1 union ...

  8. Oracle数据库锁表的查询方法以及解锁的方法

    1,锁表语句简单查询方法   select t2.username,t2.sid,t2.serial#,t2.logon_time from v$locked_object t1,v$session ...

  9. oracle数据库数据库表空间查询及扩充

    1.查询表空间,及表空间的大小 SELECT t.tablespace_name, round(SUM(bytes / (1024 * 1024)), 0) ts_size FROM dba_tabl ...

随机推荐

  1. 堆排序(php实现)

    堆排序基本步骤: 1:把无序序列构成一个堆. 2:交换堆顶元素和最后一个元素,交换之后由于堆结构破坏,重置堆. 初始化堆和交换后的重置堆区别在于:初始化堆时从最后一个非叶子结点开始调整结点位子,交换堆 ...

  2. iOS 开源库系列 Aspects核心源码分析---面向切面编程之疯狂的 Aspects

    Aspects的源码学习,我学到的有几下几点 Objective-C Runtime 理解OC的消息分发机制 KVO中的指针交换技术 Block 在内存中的数据结构 const 的修饰区别 block ...

  3. virtualbox+vagrant学习-4-Vagrantfile-5-Machine Settings

    Machine Settings 配置命名空间:config.vm config.vm的设置将修改vagrant管理的机器的配置 Available Settings可用设置 config.vm.ba ...

  4. VMware虚拟机安装Mac OS X

    安装mac系统学习网站来源:http://blog.csdn.net/hamber_bao/article/details/51335834 1.下载安装VMware workstation (1)首 ...

  5. C# 实现动态加载DLL插件 及HRESULT:0x80131047处理

    本代码实现DLL的动态加载, 类似PS里的滤镜插件! 1. 建立一个接口项目类库,此处名称为:Test.IPlugin using System; namespace Test.IPlugin { p ...

  6. 利用java代码生成keyStore

    在前面的章节中介绍了如何利用KeyTool工具生成keyStore:传送门. 但是很多时候,在javaWeb项目中,比如给每个用户加上独特的数字签名,那么我们需要在创建用户的时候,给其生成独一无二的k ...

  7. 谈谈我的js学习过程(二)——“Hello World!”

    在<谈谈我的js学习过程(一)>中,我简单聊了一下我认为的javascript的学习方法,接下来我们可以尝试来写一个最简单的js代码. "Hello World!"对于 ...

  8. Twenproxy介绍

    1 Twenproxy介绍 Twemproxy是为memcached或者Redis集群提供一个高速.轻量级代理服务.它的主要用途是降低对缓存server打开的连接数.它的主要特性例如以下: l  保持 ...

  9. boost::bind 学习

    最近学习了太多与MacOS与Iphone相关的东西,因为不会有太多人有兴趣,学习的平台又是MacOS,不太喜欢MacOS下的输入法,所以写下来的东西少了很多.    等我学习的东西慢慢的与平台无关的时 ...

  10. C语言中关于逗号运算符的理解

    在C语言中运算符有很多,包括算数运算符.关系运算符.赋值运算符.位运算符.逻辑运算符.三目运算符.sizeof运算符.逗号运算符等等,那今天我们就重点来给大家讨论一下关于逗号运算符的一些运算规则. 首 ...