2016-04-20 41 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) 
相关问题