Using Date value within Case statement.

In the below case statement I am trying to look at product that is on order to our warehouse, and have it breakout by expected arrival timeframes. I am not sure if casting the `RDC_ETA_STATUS` as a date or using a value of current date() + is a potential way of creating the ranges so any help would be appreciated.

 

(CASE
WHEN (`OW_TO_STORE` = 'X') THEN '01. Store OW'
WHEN (`DC_OH` > 0) THEN '02. RDC OH'
WHEN (`RDC_OO` > 0) and DATE(`RDC_ETA_STATUS`)=CURRENT_DATE() THEN '06a. RDC OO Due Today'
WHEN (`RDC_OO` > 0) and DATE(`RDC_ETA_STATUS`)=CURRENT_DATE()+1 THEN '06b. RDC OO Due Tomorrow'
WHEN (`RDC_OO` > 0) and DATE(`RDC_ETA_STATUS`)=CURRENT_DATE()+2 THEN '06c. RDC OO Due In 2 Days'
WHEN (`RDC_OO` > 0) and DATE(`RDC_ETA_STATUS`)>CURRENT_DATE()+2 and DATE(`RDC_ETA_STATUS`)<CURRENT_DATE()+5 THEN '06d. RDC OO Due in 3-4 days'
WHEN (`RDC_OO` > 0) and DATE(`RDC_ETA_STATUS`)>CURRENT_DATE()+4 and DATE(`RDC_ETA_STATUS`)<CURRENT_DATE()+8 THEN '06e. RDC OO Due in 5- 7 days'
WHEN (`RDC_OO` > 0) and DATE(`RDC_ETA_STATUS`)>CURRENT_DATE()+7 and DATE(`RDC_ETA_STATUS`)<CURRENT_DATE()+15 THEN '06f. RDC OO Due in 8-14 days'
WHEN (`RDC_OO` > 0) and DATE(`RDC_ETA_STATUS`)>CURRENT_DATE()+14 THEN '06g. RDC OO Due in >14 days'

WHEN (`NOTHING_AT_LOC` = 'X') THEN '10. Nothing'
ELSE 'Unclassified'

END )

Best Answer

  • ST_-Superman-_
    Answer ✓

    For your Date ranges, I would suggest usind DATEDIFF()

     

    (CASE
    WHEN (`OW_TO_STORE` = 'X') THEN '01. Store OW'
    WHEN (`DC_OH` > 0) THEN '02. RDC OH'
    WHEN (`RDC_OO` > 0) and DATEDIFF(`RDC_ETA_STATUS`,CURRENT_DATE())=0 THEN '06a. RDC OO Due Today'
    WHEN (`RDC_OO` > 0) and DATEDIFF(`RDC_ETA_STATUS`,CURRENT_DATE())=1 THEN '06b. RDC OO Due Tomorrow'
    WHEN (`RDC_OO` > 0) and DATEDIFF(`RDC_ETA_STATUS`,CURRENT_DATE())=2 THEN '06c. RDC OO Due In 2 Days'
    WHEN (`RDC_OO` > 0) and DATEDIFF(`RDC_ETA_STATUS`,CURRENT_DATE())<5 THEN '06d. RDC OO Due in 3-4 days'
    WHEN (`RDC_OO` > 0) and DATEDIFF(`RDC_ETA_STATUS`,CURRENT_DATE())<8 THEN '06e. RDC OO Due in 5- 7 days'
    WHEN (`RDC_OO` > 0) and DATEDIFF(`RDC_ETA_STATUS`,CURRENT_DATE())<15 THEN '06f. RDC OO Due in 8-14 days'
    WHEN (`RDC_OO` > 0) and DATEDIFF(`RDC_ETA_STATUS`,CURRENT_DATE())>14 THEN '06g. RDC OO Due in >14 days'

    WHEN (`NOTHING_AT_LOC` = 'X') THEN '10. Nothing'
    ELSE 'Unclassified'

    END )


    “There is a superhero in all of us, we just need the courage to put on the cape.” -Superman

Answers

  • What is the Data type for `RDC_ETA_STATUS`?

     

    Would you mind providing some sample data so we can understand your data set a little better?

     

    Is the current beastmode giving you an error message?  Or just the wrong result?


    “There is a superhero in all of us, we just need the courage to put on the cape.” -Superman
  • ST_-Superman-_
    Answer ✓

    For your Date ranges, I would suggest usind DATEDIFF()

     

    (CASE
    WHEN (`OW_TO_STORE` = 'X') THEN '01. Store OW'
    WHEN (`DC_OH` > 0) THEN '02. RDC OH'
    WHEN (`RDC_OO` > 0) and DATEDIFF(`RDC_ETA_STATUS`,CURRENT_DATE())=0 THEN '06a. RDC OO Due Today'
    WHEN (`RDC_OO` > 0) and DATEDIFF(`RDC_ETA_STATUS`,CURRENT_DATE())=1 THEN '06b. RDC OO Due Tomorrow'
    WHEN (`RDC_OO` > 0) and DATEDIFF(`RDC_ETA_STATUS`,CURRENT_DATE())=2 THEN '06c. RDC OO Due In 2 Days'
    WHEN (`RDC_OO` > 0) and DATEDIFF(`RDC_ETA_STATUS`,CURRENT_DATE())<5 THEN '06d. RDC OO Due in 3-4 days'
    WHEN (`RDC_OO` > 0) and DATEDIFF(`RDC_ETA_STATUS`,CURRENT_DATE())<8 THEN '06e. RDC OO Due in 5- 7 days'
    WHEN (`RDC_OO` > 0) and DATEDIFF(`RDC_ETA_STATUS`,CURRENT_DATE())<15 THEN '06f. RDC OO Due in 8-14 days'
    WHEN (`RDC_OO` > 0) and DATEDIFF(`RDC_ETA_STATUS`,CURRENT_DATE())>14 THEN '06g. RDC OO Due in >14 days'

    WHEN (`NOTHING_AT_LOC` = 'X') THEN '10. Nothing'
    ELSE 'Unclassified'

    END )


    “There is a superhero in all of us, we just need the courage to put on the cape.” -Superman