I've looked at a lot of similar questions asked on here and other places and I don't know how to extrapolate what they're saying to what I need, so I opted to ask for my scenario.
Basically, I need to check that the date of a row is before a certain date. The date, (this predates me and there's nothing I can do about it despite not liking the system) is stored simply as a single number which corresponds to the number of the days into the year it is. For example, Jan 1 is 1, Feb 1st is 32, etc...So, the date is figured out by adding that number to 12/31/. The problem I'm running into is that when I'm checking a period that spans New Year's, it's bringing back (for example) December 29 of THIS year, instead of LAST year. I ran into this in the SELECT and the CASE statement worked fine. In the WHERE clause, however, it's not working as expected. In the where clause, I need to check to make sure it's not a weekend. (Again, code predates me and I can't change much, sadly)
So, I now have this in the WHERE clause:
CASE
WHEN (dateadd(dd, t.periodid, '12/31/2013') > '01/24/2014') THEN AND (DATEPART(DW, convert(varchar, dateadd(dd, t.periodid, '12/31/2012'), 107)) NOT IN (1,7) OR t.periodid IS NULL)
WHEN (dateadd(dd, t.periodid, '12/31/2013') <= '01/24/2014') THEN AND (DATEPART(DW, convert(varchar, dateadd(dd, t.periodid, '12/31/2013'), 107)) NOT IN(1,7) OR t.periodid IS NULL)
END
(i.e.: If the date is greater than the last day of the period, add the number to two years ago and check if it's a weekend. Otherwise, add it to 12/31 of LAST year and check if it's a weekend.)
I think that I get that it's failing because my case should be something more like:
AND X = CASE.....
but I have no idea what to put as X in this situation.
CASE can be used in any statement or clause that allows a valid expression. For example, you can use CASE in statements such as SELECT, UPDATE, DELETE and SET, and in clauses such as select_list, IN, WHERE, ORDER BY, and HAVING.
You can use a CASE expression in any statement or clause that accepts a valid expression. For example, you can use the CASE expression in statements such as SELECT , UPDATE , or DELETE , and in clauses like SELECT , WHERE , HAVING , and ORDDER BY .
Hive supports Case statements to check the conditions against the column values. If any of the condition is true, it will stop to check the other conditions and returns the value that specified in THEN clause. In case none of the condition is met, it will return the value in the ELSE clause.
You can use case in a where , but not like that. Case has to return one value per statement.
Your where clause should look something like this if you want to use a case statement inside of it:
WHERE
(CASE
WHEN (dateadd(dd, t.periodid, '12/31/2013') > '01/24/2014') AND (DATEPART(DW, convert(varchar, dateadd(dd, t.periodid, '12/31/2012'), 107)) NOT IN (1,7) OR t.periodid IS NULL) THEN 1
WHEN (dateadd(dd, t.periodid, '12/31/2013') <= '01/24/2014') AND (DATEPART(DW, convert(varchar, dateadd(dd, t.periodid, '12/31/2013'), 107)) NOT IN(1,7) OR t.periodid IS NULL) THEN 1
ELSE 0
END) = 1
Thanks all. I actually finally figured this out, right before I came back to check out more answers.
I ended up doing this:
AND 1 =
CASE WHEN (dateadd(dd, t.periodid, '12/31/2013') > '01/24/2014') AND (DATEPART(DW, convert(varchar, dateadd(dd, t.periodid, '12/31/2012'), 107)) NOT IN(1,7) OR t.periodid IS NULL)
THEN 1
WHEN (dateadd(dd, t.periodid, '12/31/2013') <= '01/24/2014') AND (DATEPART(DW, convert(varchar, dateadd(dd, t.periodid, '12/31/2013'), 107)) NOT IN(1,7) OR t.periodid IS NULL)
THEN 1
END
which seems to work!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With