Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 使用Oracle11g联合所有查询_Sql_Oracle11g - Fatal编程技术网

Sql 使用Oracle11g联合所有查询

Sql 使用Oracle11g联合所有查询,sql,oracle11g,Sql,Oracle11g,我们每15分钟运行一次,如果在表1中创建了任何新行,则执行UNIONALL查询 现在我在做这个, select x,y from table 1, table 2 where table1.location = 'IL' and table1.col1 = table2.col1 union all select x,y from table 1, table 3 where table1.location = 'NY' and table1.col2 = table3.col2 unio

我们每15分钟运行一次,如果在表1中创建了任何新行,则执行UNIONALL查询

现在我在做这个,

select x,y
from table 1, table 2
where table1.location = 'IL' and table1.col1 = table2.col1 
union all 
select x,y
from table 1, table 3
where table1.location = 'NY' and table1.col2 = table3.col2 
union all 
select x,y
from table 1, table 4
where table1.location = 'KY' and table1.col1 = table4.col1 
union all 
select x,y
from table 1, table 5
where table1.location = 'TX' and table1.col1 = table5.col1 
假设表1中有10行是在15分钟的时间间隔内创建的,其中

“IL”中的
2行
“纽约”有2行
“KY”中有2行
和“TX”中的4行

使用oracle 11g是否有任何方法,我只能将“IL”第一个查询中的2行、从“NY”到第二个查询中的2行和从“TX”到第四个查询中的4行传递给union all查询中的每个条件

在将行传递到表之前,可以考虑使用“WITH子句”将行配对。例如:

select * from
(select id,x,y from table 1, table 2 where table1.location = 'IL' and table1.col1 = table2.col1 

union all 

select id,x,y from table 1, table 3 where table1.location = 'NY' and table1.col2 = table3.col2 

union all 

select id,x,y from table 1, table 4 where table1.location = 'KY' and table1.col1 = table4.col1 

union all 

select id,x,y from table 1, table 5 where table1.location = 'TX' and table1.col1 = table5.col1) usr1, 
(select id,x,y,location from table 1 where create_date >= SYSDATE - 15 / 1440) usr2, 
where usr2.location in ('IL','NY','KY','TX') and usr1.id = usr2.id 

我假设优化器足够聪明,只对
执行一次
WITH
查询,并在所有联合查询中重复使用结果。

在将行传递到表之前,不妨看看使用“WITH子句”将行配对。例如:

select * from
(select id,x,y from table 1, table 2 where table1.location = 'IL' and table1.col1 = table2.col1 

union all 

select id,x,y from table 1, table 3 where table1.location = 'NY' and table1.col2 = table3.col2 

union all 

select id,x,y from table 1, table 4 where table1.location = 'KY' and table1.col1 = table4.col1 

union all 

select id,x,y from table 1, table 5 where table1.location = 'TX' and table1.col1 = table5.col1) usr1, 
(select id,x,y,location from table 1 where create_date >= SYSDATE - 15 / 1440) usr2, 
where usr2.location in ('IL','NY','KY','TX') and usr1.id = usr2.id 

我假设优化器足够聪明,只对
查询执行一次
,并在所有联合查询中重复使用结果。

请为您提出的所有问题选择答案。请为您提出的所有问题选择答案。