Důvodem, proč se ve vašem dotazu objevila chyba, je to, že před definováním dílčího dotazu musíte uvést, co z něj chcete vybrat. Takže pokud jste to předponovali select * from
byl by to platný dotaz.
Všimněte si, že tyto or
nemusíte dělat operace, protože to můžete udělat kratší s in
operátor.
Měli byste také negovat některá srovnání (protože již máte NOT
) a zkraťte data pomocí TRUNC
.
Zde je dotaz, který bych navrhl:
SELECT TEMP.REG_ID,
TEMP.EVENT_TYPE,
TEMP.EVENT_DATE,
TEMP.PRODUCT_CD,
TEMP.TERM_START_DATE,
CASE WHEN TEMP.EVENT_TYPE IN ('NEW SUBSCRIPTION', 'RENEWAL', 'UPSELL')
AND TEMP.NEXT_EVENT_TYPE = 'CANCELLATION' THEN
LEAST(TEMP.TERM_END_DATE, TEMP.NEXT_TERM_END_DATE)
ELSE TEMP.TERM_END_DATE
END AS TERM_END_DATE,
TEMP.DAYS,
TEMP.AMT
FROM (SELECT REG_ID,
EVENT_TYPE,
EVENT_DATE,
PRODUCT_CD,
TERM_START_DATE,
TERM_END_DATE,
DAYS,
AMT,
LAG(EVENT_TYPE, 1, '-') over (
PARTITION BY REG_ID, PRODUCT_CD
ORDER BY EVENT_DATE, TERM_START_DATE) as PREV_EVENT_TYPE,
LAG(EVENT_DATE, 1) over (
PARTITION BY REG_ID, PRODUCT_CD
ORDER BY EVENT_DATE, TERM_START_DATE) as PREV_EVENT_DATE,
LEAD(EVENT_TYPE, 1, '-') over (
PARTITION BY REG_ID, PRODUCT_CD
ORDER BY EVENT_DATE, TERM_START_DATE) as NEXT_EVENT_TYPE,
LEAD(EVENT_DATE, 1) over (
PARTITION BY REG_ID, PRODUCT_CD
ORDER BY EVENT_DATE, TERM_START_DATE) as NEXT_EVENT_DATE,
LEAD(TERM_END_DATE, 1) over (
PARTITION BY REG_ID, PRODUCT_CD
ORDER BY EVENT_DATE, TERM_START_DATE) as NEXT_TERM_END_DATE
FROM export_table) TEMP
WHERE NOT (TEMP.EVENT_TYPE = 'CANCELLATION'
AND TEMP.PREV_EVENT_TYPE IN ('NEW SUBSCRIPTION', 'RENEWAL', 'UPSELL')
AND TRUNC(TEMP.EVENT_DATE) = TRUNC(TEMP.PREV_EVENT_DATE))
AND NOT (TEMP.NEXT_EVENT_TYPE = 'CANCELLATION'
AND TEMP.EVENT_TYPE IN ('NEW SUBSCRIPTION', 'RENEWAL', 'UPSELL')
AND TRUNC(TEMP.NEXT_EVENT_DATE) = TRUNC(TEMP.EVENT_DATE))
Všimněte si, že term_end_date záznamu 6 je také upraven, protože se na něj vztahuje pravidlo 2.