Oracle SQL- Flag records based on record's date vs history

后端 未结 4 423
伪装坚强ぢ
伪装坚强ぢ 2021-01-27 06:01

This is my 1st post on the forum. Usually I was able to find what I needed - but to tell the truth - I am not really sure how to ask a correct question to the issue. Therefore,

相关标签:
4条回答
  • 2021-01-27 06:29

    You can use LAG function:

    SELECT
    "REGION"
    , "COUNTRY"
    , "IDNum"
    , "CUSTOMER"
    , "BUSSINESS"
    , "FISCALYEARMONTH"
    , "FISCALYEAR"
    , SUM("VALUE") 
    , MAX(CASE WHEN to_date(prev_fym,'YYYYMM')  >= ADD_MONTHS (to_date("FISCALYEARMONTH",'YYYYMM'), -24) THEN 'Existing' 
               ELSE NULL END) "HISTORICAL_PURCHASE_FLAG"
    FROM
    (
        SELECT
        T1."REGION"
        , T1."COUNTRY"
        , T1."IDNum"
        , T1."CUSTOMER"
        , T1."BUSSINESS"
        , T3."FISCALYEARMONTH"
        , T3."FISCALYEAR"
        , T4."VALUE"
        , LAG ("FISCALYEARMONTH", 1) OVER (PARTITION BY T1."IDNum" ORDER BY T3."FISCALYEARMONTH" DESC) prev_fym 
        FROM
        "DATABASE"."SALES" T4
        , "DATABASE"."CUSTOMER" T1
        , "DATABASE"."PRODUCT" T2
        , "DATABASE"."TIME" T3
        WHERE
        T4."CUSTOMERID" = T1."CUSTOMERID"
        AND T4."PRODUCTID" = T2."PRODUCTID"
        AND T4."DATEID" = T3."DATEID"
        AND T1."COUNTRY" IN ('ENGLAND', 'France')
        AND T3."FISCALYEAR" IN ('2014','2015','2016')
    )
    WHERE "FISCALYEAR" IN ('2016')
    GROUP BY
    "REGION"
    , "COUNTRY"
    , "IDNum"
    , "CUSTOMER"
    , "BUSSINESS"
    , "FISCALYEARMONTH"
    , "FISCALYEAR"
    ;
    
    0 讨论(0)
  • 2021-01-27 06:33

    Using a simplified "input" table... You can use the LAG() analytic function and a comparison condition to populate your last column. I assume your fiscalyearmonth is a number - if it is a character field, wrap fiscalyearmonth within TO_NUMBER(). (It would be much better if in fact you stored these as true Oracle dates, perhaps date 2016-06-01 instead of 201606, but I worked with what you have currently... and took advantage that in numeric format, "24 months ago" simply means "subtract 200").

    with inputs (linenum, idnum, fiscalyearmonth) as (
           select 1, 255, 201605 from dual union all
           select 2, 123, 201602 from dual union all
           select 3, 255, 201601 from dual union all
           select 4, 255, 201210 from dual
         )
    select linenum, idnum, fiscalyearmonth, 
           case when fiscalyearmonth 
                     - lag(fiscalyearmonth) 
                               over (partition by idnum order by fiscalyearmonth) < 200
                then 'Existing' else 'New' end         as flag
    from   inputs
    order by linenum;
    
    
       LINENUM      IDNUM FISCALYEARMONTH FLAG
    ---------- ---------- --------------- --------
             1        255          201605 Existing
             2        123          201602 New
             3        255          201601 New
             4        255          201210 New
    
    0 讨论(0)
  • 2021-01-27 06:38

    You can achieve using row_number() function as below... modify as per your need...I assumed 2 years (means previous 24 months from sysdate ). You can run the sub-queries separately to check how its working.

    Select 
    "REGION"
    ,"COUNTRY"
    ,"IDNum"
    ,"CUSTOMER"
    ,"BUSSINESS"
    ,"FISCALYEARMONTH"
    ,"FISCALYEAR"
    ,"VALUE"
    , ( case when ( TXNNO = 1 or TOTAL_TXN_LAST24MTH = 0 ) then 'New' else 'Existing' end ) as "HISTORICAL_PURCHASE_FLAG"  -- if no txn in last 24 month or its first txn then 'new' else 'existing'
    from
    (
      select 
      SubQry."REGION"
      , SubQry."COUNTRY"
      , SubQry."IDNum"
      , SubQry."CUSTOMER"
      , SubQry."BUSSINESS"
      , SubQry."FISCALYEARMONTH"
      , SUBQRY."FISCALYEAR"
      , SUBQRY."VALUE"
      , ROW_NUMBER() over (partition by SUBQRY."REGION",SUBQRY."COUNTRY",SUBQRY."IDNum",SUBQRY."CUSTOMER",SUBQRY."BUSSINESS" order by SUBQRY."FISCALYEARMONTH") as TXNNO,
      , SUM(case when (TO_NUMBER(TO_CHAR(sysdate,'YYYYMM')) - SUBQRY."FISCALYEARMONTH") < 24 then 1 else 0 end) as TOTAL_TXN_LAST24MTH
      From
      (
        SELECT
        T1."REGION"
        , T1."COUNTRY"
        , T1."IDNum"
        , T1."CUSTOMER"
        , T1."BUSSINESS"
        , T3."FISCALYEARMONTH"
        , T3."FISCALYEAR"
        , SUM(T4."VALUE") as VALUE
        FROM
        "DATABASE"."SALES" T4
        , "DATABASE"."CUSTOMER" T1
        , "DATABASE"."PRODUCT" T2
        , "DATABASE"."TIME" T3
        WHERE
        T4."CUSTOMERID" = T1."CUSTOMERID"
        AND T4."PRODUCTID" = T2."PRODUCTID"
        AND T4."DATEID" = T3."DATEID"
        AND T3."FISCALYEAR" IN ('2016')
        AND T1."COUNTRY" IN ('ENGLAND', 'France')
        GROUP BY
        T1."REGION"
        , T1."COUNTRY"
        , T1."IDNum"
        , T1."CUSTOMER"
        , T1."BUSSINESS"
        , T3."FISCALYEARMONTH"
        , T3."FISCALYEAR"
      ) SUBQRY
    );
    
    0 讨论(0)
  • 2021-01-27 06:47

    Another solution might be to outer Join "DATABASE"."SALES" T4 a second time as T5, wilter the fiscal year via WHERE to < t4.FiscalYear-2. If the Column is NULL, the record is new, if the outer join results in a value, the record is historic.

    0 讨论(0)
提交回复
热议问题