简体   繁体   English

我想要一种将所有这些Oracle查询合并为一个的方法

[英]I want a way to combine all these Oracle queries into one

 select 
 UNIT,
 NBR, 
 ODE,
 ANS,
 IT,
 UNIT,
 ESC,
DATE1,
DATE2,
CD,
CD2,
CD3,
TRANS
from Jblog
where UNIT = 'Alaska'
AND DATE1 > 0 and
DATE2 > 0
AND ODE = '67342'
AND NBR = '50952'



select
 UNIT,
 TRANS,
 ELER, 
 ELER_DATE,
 ELERDATE2, 
 TRANS
from JBLOG
where unit = 'ALASKA' 
AND ELER <> ' ' 
and ELERDATE > 0 and
ELERDATE2 > 0
and ELER = '5201'
 
select
 UNIT,
 TRANS,
 LNT,
 LNT_MIN, 
 LNT_MAX,
 LNT_D, 
 LNT_DATE1,
 LNT_DATE2,
 LNT_DATE3,
 LNT_AL,
 TRANS
from JBLOG
where UNIT = 'ALASKA'
AND LNT_DATE <> 0
AND LNT_DATE2 > 0 and
LNT_DATE3> 0
AND LNT_D = '0064'

 

I want to be able to combine all these queries and run them as one. 我希望能够合并所有这些查询并将它们作为一个查询运行。 The data is different so if I just straight up combine them and have one long where clause with a bunch of ands I won't get any results because the exclusion will make it not return any data. 数据是不同的,因此,如果我直接将它们组合在一起,并在一个long where子句中加上一堆ands,我将不会得到任何结果,因为排除将使其不返回任何数据。 However, when I run them individually I get the desired result. 但是,当我单独运行它们时,会得到理想的结果。 I tried unions and it did not quite work out, does anyone have an solution? 我尝试了工会,但效果不佳,有人解决方案吗? Thanks in advance 提前致谢

从表中以相同顺序选择所有列,然后使用并集将它们合并,从而产生所需的结果。

UNION should solve your problem, but note that UNION requires that the queries to be combined must return an equal sequence of attributes (concerning name and type). UNION应该可以解决您的问题,但是请注意, UNION要求要合并的查询必须返回相等的属性序列(关于名称和类型)。

So when combining above queries with UNION , each of the queries must select the same sequence of attributes; 因此,将上述查询与UNION结合使用时,每个查询都必须选择相同的属性序列; and if particular attributes make no sense in a particular query, you must still provide them, even if you chose to select a default value. 并且如果特定属性在特定查询中没有意义,那么即使您选择选择默认值,您仍必须提供它们。

For example, the following query should work: 例如,以下查询应该工作:

select a, b, NULL as c
  from table1
  where a>5
union
select a, NULL as b, c
  from table1
  where a<=5

whereas the following query doesn't: 而以下查询则不会:

select a, b
  from table1
  where a>5
union
select a, c
  from table1
  where a<=5

There has to be some kind of key to join by. 必须有某种钥匙才能加入。 Pick whatever fields make a unique item key and do a LEFT JOIN to a master list for each of those queries on those fields. 选择任何构成唯一项目键的字段,并针对这些字段中的每个查询向主列表进行LEFT JOIN。

Since your queries filter out and won't necessarily have proper joins for everything, add another level to the query that picks everything without any filter so you can join everything to it: 由于您的查询会被过滤掉,并且不一定对所有内容都具有适当的联接,因此可以在查询中添加另一个级别来选择所有内容而无需任何过滤器,因此您可以将所有内容都加入其中:

select * 
from 
    (select UNIT /*or key whatever*/ from Jblog) all_units
LEFT JOIN
     (.....) a
ON a.unit = all_units.unit
LEFT JOIN
     (.....) b
ON b.unit = all_units.unit
LEFT JOIN
     (.....) c
ON c.unit = all_units.unit

where a, b, and c are your 3 queries you listed 其中a,b和c是您列出的3个查询

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

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