2017-07-17 10 views
0

選択クエリで条件付き計算列を作成しようとしています。論理は以下の通りですPL/SQL:選択クエリで条件付き計算列を作成する必要があります

Select FT.NAME, 
    COUNT(DISTINCT PAGEID) as Total_Forms, 
    COUNT(DISTINCT PAGEID WHERE FT."Column1?" =1) as Expected_forms, 
    COUNT(DISTINCT PAGEID WHERE FT."Column1?" =1 AND FT."FORM STATUS" in ('C','I') as Actual_forms, 
    COUNT(DISTINCT DATAPAGEID WHERE FT."IS FORM EXPECTED1?" =1)/ 
    COUNT(DISTINCT DATAPAGEID WHERE FT."IS FORM EXPECTED1?" =1 AND FT."FORM STATUS" in ('C','I')) 
    as Percentage 
    FROM 
     (Subquery) FT 
    Group by FT.Name 

私は様々な組み合わせを試しましたが、これまでのところ何も働いていません。出力は次のようになります

name  Total_forms Expected_forms Actual Forms Percentage 
    abc  943   811    781   96.54% 
    pqr  900   800    600   75.00% 

ただし、これをPL/SQLクエリで作成することはできません。どんな助けも大歓迎です。

答えて

1

この構文が間違っている:代わりに

COUNT(DISTINCT PAGEID WHERE FT."Column1?" =1) as Expected_forms, 

使用CASE Expression

SELECT 
    COUNT(DISTINCT PAGEID) as Total_Forms, 
    COUNT(DISTINCT CASE WHEN FT."Column1?" =1 THEN PAGEID END) as Expected_forms, 
    COUNT(DISTINCT CASE WHEN FT."Column1?" =1 AND FT."FORM STATUS" in ('C','I') 
        THEN PAGEID END) as Actual_forms, 
    COUNT(DISTINCT CASE WHEN FT."IS FORM EXPECTED1?" =1 THEN DATAPAGEID END)/ 
    COUNT(DISTINCT CASE WHEN FT."IS FORM EXPECTED1?" =1 AND FT."FORM STATUS" in ('C','I') 
        THEN DATAPAGEID END) as Percentage 
    FROM 
     (Subquery) FT 
    Group by FT.Name 
関連する問題