根据两个数据库表之间的数据比较创建oracle视图

create oracle view based on comparision of data between two database tables(根据两个数据库表之间的数据比较创建oracle视图)
本文介绍了根据两个数据库表之间的数据比较创建oracle视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有下表:

  • 我想创建视图,以便对于 descr = 'O' 和两个表中的公共 id_isin 字段值,检查 ratio字段并仅取 ratio 字段值较低的行.
  • 对于 descr = 'O' 并且如果 id_isin 存在于一个表中但不在另一个表中,则取那些行(双向)
  • 对于 descr != 'O',从表 IS_ID_TST 中取出所有这些行.
  • I want to create View such that for descr = 'O' and for common id_isin field value from both tables, check the ratio field and take only the row where ratio field value is low.
  • for descr = 'O' and if the id_isin exist in one table but not in another then take those rows(bidirectional)
  • For all the rows where descr ! = 'O', take all those rows from table IS_ID_TST.

以下是视图的预期输出,例如:

Below is the expected output from view for example:

ID_ISIN   QUOTE_CRNY   DESCR           RATIO              ALLOCATIONASSETTYPE
L000123    USD              O              0.0769          Other total
L000129    USD              O              0.0669          Other total
D123458    USD              O              0.64039         Other total
M123456    USD              O              5.64039         Other total
F563458    USD              C              0.84039         Other total
G123456    USD              null           0.04039         Other total
L000123    USD              C              5.0769          Other total

我可以根据这个条件创建视图吗?

Can i create View based on this conditions ?

推荐答案

我提出了以下查询,请检查我已对查询发表评论.询问是否不符合您的要求或进行任何澄清.

I have come up with below query, Please check I have put comments on the query. Ask if doesn't meet you r requirement or for any clarification.

CREATE VIEW v_combined_data AS
WITH combined_data
AS
(
SELECT t1.fund_isin
      ,t1.fund_quote_crny
      ,t1.member_descr
      ,t1.member_ratio
      ,t1.allocationassettype
      ,t2.fund_isin fund_isin_tst
      ,t2.fund_quote_crny fund_quote_crny_tst
      ,t2.member_descr member_descr_tst
      ,t2.member_ratio member_ratio_tst
      ,t2.allocationassettype allocationassettype_tst
FROM   is_id t1
FULL   OUTER JOIN is_id_tst t2
ON     t1.fund_isin = t2.fund_isin
AND    t1.fund_quote_crny = t2.fund_quote_crny
AND    t1.member_descr = t2.member_descr
)
-- for member_descr = 'O' and for common fund_isin field value from both tables, 
-- check the member_ratio field and take only the row where member_ratio field value is low.
SELECT d.fund_isin
      ,d.fund_quote_crny
      ,d.member_descr
      ,LEAST(d.member_ratio,d.member_ratio_tst) member_ratio
      ,d.allocationassettype
  FROM combined_data d
WHERE d.member_descr = 'O'
  AND d.fund_isin IS NOT NULL 
  AND d.fund_isin_tst IS NOT NULL
UNION ALL
--for member_descr = 'O' and if the fund_isin exist in one table but not in another then take those rows(bidirectional)
--exists in IS_ID and not in IS_ID_TST
SELECT d.fund_isin
      ,d.fund_quote_crny
      ,d.member_descr
      ,d.member_ratio
      ,d.allocationassettype
  FROM combined_data d
WHERE d.member_descr = 'O'
  AND NOT EXISTS (SELECT 1
                    FROM combined_data ci
                   WHERE ci.fund_isin_tst = d.fund_isin
                     AND ci.fund_quote_crny_tst = d.fund_quote_crny
                     AND ci.member_descr_tst = 'O')
UNION ALL
--for member_descr = 'O' and if the fund_isin exist in one table but not in another then take those rows(bidirectional)
--exists in IS_ID_TST and not in IS_ID
SELECT d.fund_isin_tst
      ,d.fund_quote_crny_tst
      ,d.member_descr_tst
      ,d.member_ratio_tst
      ,d.allocationassettype_tst
  FROM combined_data d
WHERE d.member_descr_tst = 'O'
  AND NOT EXISTS (SELECT 1
                    FROM combined_data ci
                   WHERE ci.fund_isin = d.fund_isin_tst
                     AND ci.fund_quote_crny = d.fund_quote_crny_tst
                     AND ci.member_descr = 'O')
UNION ALL
--for all the rows where member_descr ! = 'O', take all those rows from table IS_ID_TST
SELECT d.fund_isin_tst
      ,d.fund_quote_crny_tst
      ,d.member_descr_tst
      ,d.member_ratio_tst
      ,d.allocationassettype_tst
  FROM combined_data d
WHERE d.fund_isin_tst IS NOT NULL
  AND (d.member_descr_tst != 'O' OR d.member_descr_tst IS NULL);

这篇关于根据两个数据库表之间的数据比较创建oracle视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Sending parameters to stored procedures vb.net(将参数发送到存储过程 vb.net)
Insert multiple rows, count based on another table columns(插入多行,根据另一个表列计数)
How to hold the location of an image in a SQL Server database?(如何在 SQL Server 数据库中保存图像的位置?)
How to send to email (outlook) the selected items in SQL Server database using vb.net(如何使用 vb.net 将 SQL Server 数据库中的选定项目发送到电子邮件(Outlook))
datagrid checkbox writes Null instead of 0 (false) into database(datagrid 复选框将 Null 而不是 0 (false) 写入数据库)
DBCC CheckDb-any ways to detect errors vb.net?(DBCC CheckDb-vb.net 有什么检测错误的方法吗?)