2015-03-13 60 views
-1

我有一個SQL查詢,我在下面提到過,但是我使用了需要更多時間執行的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

這是無效的SQL,您在選擇列表中有既不是聚合函數參數也不在GROUP BY子句中列出的列。 – jarlh 2015-03-13 12:23:46

+0

這是怎麼回事?您沒有彙總值。您的查詢沒有任何意義。 – Bohemian 2015-03-13 12:25:38

+0

這不是真正的查詢..實際查詢是巨大的..但它在選擇列表中有source_type ... – user2642751 2015-03-13 12:29:50

回答

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一個在任何一套基於語言得到結果最快方式。