Teradata

在一個請求中合併多個選擇

  • December 6, 2019

我必須使用基於相同條件的選擇語句來提取數據,但需要稍作改動,我想在一個語句中為我的所有選擇進行聯合:

select   Date_,  application_name,count(*)   total1  
from   PDETAIL
where Application_Name='My_Apllication' and  
SUBSTR(Statistics_Variables,36,1)='1'  and
File_Arriving_Date between date-29 and date-1
group by 1,2   

select   Date_,  application_name,count(*)   total2  
from   PDETAIL
where Application_Name='My_Apllication' and  
SUBSTR(Statistics_Variables,38,1)='1'  and
File_Arriving_Date between date-29 and date-1
group by 1,2   
SELECT Date_,
      application_name,
      SUM(SUBSTR(Statistics_Variables,36,1)='1') total1,  
      SUM(SUBSTR(Statistics_Variables,38,1)='1') total2
FROM pdetail
WHERE Application_Name='My_Apllication' 
 AND File_Arriving_Date BETWEEN DATE-29 AND DATE-1
GROUP BY 1,2  

如果查詢會產生“語法錯誤”(DBMS 不是 MySQL,因為問題已被標記),那麼

SELECT Date_,
      application_name,
      COUNT(CASE WHEN SUBSTR(Statistics_Variables,36,1)='1' THEN 1 END) total1,  
      COUNT(CASE WHEN SUBSTR(Statistics_Variables,38,1)='1' THEN 1 END) total2
FROM pdetail
WHERE Application_Name='My_Apllication' 
 AND File_Arriving_Date BETWEEN DATE-29 AND DATE-1
GROUP BY 1,2  

引用自:https://dba.stackexchange.com/questions/254861