Search code examples
sqlselectteradatacase-when

teradata SQL CASE WHEN - SELECT Failed. 3771: Illegal expression in WHEN clause of CASE expression


I'm trying to group data by diffrent kinds of time periods and I'm failing to do so. I would like to name those time periods as 'onboarding' and 'in-life' but Terdata won't let me. Anybody know why it just keeps sreaming that my when clause is illegal?

SELECT
CASE 
MONTHS_BETWEEN (business_dt, CAST(LOYALITY_START_DATE_CRM AS DATE))
WHEN 
MONTHS_BETWEEN (business_dt, CAST(LOYALITY_START_DATE_CRM AS DATE)) > 0 
AND
MONTHS_BETWEEN (business_dt, CAST(LOYALITY_START_DATE_CRM AS DATE)) < 3 then 'onboarding' 
WHEN 
MONTHS_BETWEEN (business_dt, CAST(LOYALITY_START_DATE_CRM AS DATE)) > 3 
AND 
MONTHS_BETWEEN (business_dt, CAST(LOYALITY_START_DATE_CRM AS DATE)) < 18 then 'in-life' 
else 'eop'
END AS life_cycle,
COUNT (*) Number_of_contracts
FROM
VT_DM_CUMULATIVE_EXT.mobile_events
GROUP BY
life_cycle
WHERE
business_dt='2020-11-30'
;

PLS HELP


Solution

  • You are combining simple and searched case statements. Get rid of the first MONTHS_BETWEEN clause.

    SELECT
    CASE 
    WHEN 
    MONTHS_BETWEEN (business_dt, CAST(LOYALITY_START_DATE_CRM AS DATE)) > 0 
    AND
    MONTHS_BETWEEN (business_dt, CAST(LOYALITY_START_DATE_CRM AS DATE)) < 3 then 'onboarding'
    ...