如何在SQL中以不同的方式使用UNION ALL概念

时间:2015-03-13 12:19:57

标签: sql oracle union-all

我有一个SQL查询,我在下面提到,但我使用了UNION ALL,这需要更多的时间来执行。但我想要一些不同的方法来获得更高效的相同细节。请帮帮我。

select creation_time, collected, errored
from batch_summary
where creation_time < (SYSDATE -1/24) and source_type in ('A','B','c')
group by creation_time, source_type

union all

select creation_time, collected, errored
from batch_summary
where creation_time < (SYSDATE -1/24) and source_type in ('d') and batch_id like '%PGW%'
group by creation_time, source_type

union all

select creation_time, collected, errored
from batch_summary
where creation_time < (SYSDATE -1/24) and source_type in ('E','F')
group by creation_time, source_type

1 个答案:

答案 0 :(得分:0)

将条件放在OR

select creation_time, collected, errored
from batch_summary
where 

(creation_time < (SYSDATE -1/24) and source_type in ('A','B','c'))
OR
(creation_time < (SYSDATE -1/24) and source_type in ('d') and batch_id like '%PGW%')
OR
(creation_time < (SYSDATE -1/24) and source_type in ('E','F'))
group by creation_time, source_type

顺便说一下,UNION ALL 是以任何基于集合的语言获取结果的最快方式之一。