简体   繁体   English

将 5 个查询合二为一

[英]Consolidating 5 Queries into One

I have two pieces of data that I have analysed and found five distinct ways in which the data is linked.我分析了两条数据,发现了五种不同的数据链接方式。 Is it possible to combine these into one query?是否可以将这些组合成一个查询? Combining queries like these is outside my skill set, but getting these queries into a singular one would be a huge help for what I am trying to accomplish.组合这样的查询超出了我的技能范围,但是将这些查询变成一个单一的查询将对我想要完成的工作有很大的帮助。

Here are the queries (Actual Table and Column names have been replaced)以下是查询(实际表名和列名已被替换)

SELECT a.Col1, d.Col2 FROM Table1 A
RIGHT JOIN Table2 B ON A.Col1=B.Col3
LEFT JOIN Table3 C ON B.Col4=C.Col5
LEFT JOIN Table4 D ON D.Col2=C.Col6;

SELECT a.Col1, d.Col2 FROM Table1 A
RIGHT JOIN Table2 B ON A.Col1=B.Col3
LEFT JOIN Table5 E ON E.Col14=B.Col4  AND Col7='Value1'
LEFT JOIN Table6 F ON E.Col8=F.Col9
LEFT JOIN Table3 C ON F.Col9=C.Col5
LEFT JOIN Table4 D ON D.Col2=C.Col6 ;

SELECT a.Col1, d.Col2 FROM Table1 A
RIGHT JOIN Table2 B ON A.Col1=B.Col3
LEFT JOIN Table7 E ON E.Col10=B.Col4  AND Col7='Value2'
LEFT JOIN Table6 F ON E.Col11=F.Col9
LEFT JOIN Table3 C ON F.Col9=C.Col5
LEFT JOIN Table4 D ON D.Col2=C.Col6 C;

SELECT a.Col1, d.Col2 FROM Table1 A
RIGHT JOIN Table2 B ON A.Col1=B.Col3
LEFT JOIN Table7 F ON F.Col10=b.Col4 AND Col7='Value3'
LEFT JOIN Table3 C ON F.Col11=C.Col5
LEFT JOIN Table4 D ON D.Col2=C.Col6 ;

SELECT a.Col1, d.Col2 FROM Table1 A
RIGHT JOIN Table2 B ON A.Col1=B.Col3
LEFT JOIN Table8 E ON E.Col12=B.Col4 AND Col7='Value4'
LEFT JOIN Table6 F ON E.Col13=F.Col9
LEFT JOIN Table3 C ON F.Col9=C.Col5
LEFT JOIN Table4 D ON D.Col2=C.Col6 ;

The result of the queries should give data like below.查询的结果应提供如下数据。 For any value in Col1 there could be multiple values in Col2, however, for each Col1/Col2 pairing, only 1 set of the queries above creates the link between the two entities.对于 Col1 中的任何值,Col2 中可能有多个值,但是,对于每个 Col1/Col2 配对,只有上述一组查询创建了两个实体之间的链接。

Col1 | Col2
-----------
  1  |  A
  2  |  B
  2  |  C
  3  |  D
  4  |  A

Thank you for any assistance.感谢您提供任何帮助。 Let me know if have any questions on the queries or results.如果对查询或结果有任何疑问,请告诉我。

Note - These queries are being executed against an Oracle database.注意 - 这些查询是针对 Oracle 数据库执行的。

Use:利用:

SELECT a.Col1, d.Col2 
FROM Table2 B
LEFT JOIN Table1 A ON A.Col1=B.Col3 
LEFT JOIN Table5 H ON H.Col14=B.Col4 AND Col7='Value1'
LEFT JOIN Table7 E ON (E.Col10=B.Col4 AND Col7='Value2') OR (F.Col10=b.Col4 AND Col7='Value3')
LEFT JOIN Table6 F ON (E.Col13=F.Col9) OR (E.Col8=F.Col9) OR (E.Col11=F.Col9) OR (H.Col8=F.Col9)
LEFT JOIN Table3 C ON (B.Col4=C.Col5) OR (F.Col9=C.Col5) OR (F.Col11=C.Col5)
LEFT JOIN Table4 D ON D.Col2=C.Col6;

or a UNION operator for your select statements.或您的 select 语句的 UNION 运算符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM