案例SQL如果日期

时间:2016-04-20 18:50:08

标签: sql teradata

我是SQL的新手,我正在努力解决一个案例。 我想假设一个帐户(account_ID)没有current_date-302和current_date-62之间的记录(ON billing_id)那么标记为“1”

以下查询:

提前致谢

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

2 个答案:

答案 0 :(得分: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 ...

答案 1 :(得分:0)

您需要应用这样的聚合函数:

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