2016-04-20 71 views
0

我是SQL新手,正在努力應對一個案例。 我想使其中如果一個帳戶(ACCOUNT_ID)不具有一個記錄(ON BILLING_ID)CURRENT_DATE-302和CURRENT_DATE-62之間的情況下然後標記爲「1」案例SQL如果在日期之間沒有記錄

以下查詢:

在此先感謝

SELECT 
    billing_date_local_time 
    ,account_id 
    ,contract_owner_name 
    ,date_first_feature_partner 
    ,deal_starts_at 
    ,contract_id 
    ,new_partner_type 
    ,sum(voucher_sold) AS Vouchers 
    ,sum(gross_bookings_local) AS GB 
    ,sum(gross_revenue_local) AS GR 
    ,is_G2 
    ,Case when billing_date_local_time between current_date-302 and current_date-62 = 0 THEN 'YES' ELSE 'NO' End 
FROM EMEA_ANALYTICS.eu_deal_flat 
WHERE 
     country_id = 206 
    and billing_date_local_time between current_date-400 
    and current_date-2 

GROUP BY 1,2,3,4,5,6,10,11 
+1

刪除= 0 CURRENT_DATE-62後。這解決了你的問題。 –

+0

你究竟是什麼意思*標記爲「1」*? – Bohemian

回答

0

你需要做一個相關的子查詢;是這樣的:

select 
a.billing_date_local_time 
,a.account_id 
,... 
, CASE WHEN EXISTS (SELECT * FROM EMEA_ANALYTICS.eu_deal_flat b WHERE a.account_id = b.account_id AND b.billing_date_local_time between current_date-302 and current_date-62) THEN 'YES' ELSE 'NO' END 
from 
FROM EMEA_ANALYTICS.eu_deal_flat a 
WHERE ... 
+0

謝謝 - 但是當我更新查詢建議我得到一個錯誤選擇失敗3771非法表達式時case表達式的條款 - 任何想法? –

+0

Im使用Teradata如果有任何區別。 –

0

您需要申請一個聚合函數是這樣的:

min(case when billing_date_local_time 
       between current_date-302 and current_date-62 
     then 0 
     else 1 
    end) 
相關問題