That should work. The where section is performed before the select section so you can’t refer to case statement results in the where section (unless you’re in an outer query)
HAVING statements act on the final table, so I do believe it could be used in a “where” type situation on the output.
Although, I do believe that the query execution is more efficient when “filtering” can be done on the input tables; but it may not be as clear to a human reader as to what’s going on.
SELECT
...
CONCAT('123',
CASE
WHEN [PRODUCT] IN ('AAA','BBB','CCC') THEN 'ABC'
WHEN LEFT([PRODUCT],2) = 'PO' AND [LINE_OF_BUSINESS] = 'Q' THEN 'ABC'
WHEN [PRODUCT] IN ('XXX','YYY','ZZZ') THEN 'XYZ'
ELSE '???'
END
) AS KOODI
It gets hung up on the " WHEN LEFT([PRODUCT],2) = ‘PO’ AND [LINE_OF_BUSINESS] = ‘Q’ THEN ‘ABC’" line telling me “Incorrect syntax near the keyword ‘THEN’.”
I’ve also tried to use OR but get the same error message:
SELECT
...
CONCAT('123',
CASE
WHEN [PRODUCT] IN ('AAA','BBB','CCC')
OR LEFT([PRODUCT],2) = 'PO' AND [LINE_OF_BUSINESS] = 'Q' THEN 'ABC'
WHEN [PRODUCT] IN ('XXX','YYY','ZZZ') THEN 'XYZ'
ELSE '???'
END
) AS KOODI
What I’m really trying to accomplish: I have a whole bunch of "PRODUCT"s that are mapped to KOODI. There are some PRODUCTs, however, that are mapped based on the combination of partial-PRODUCT and their LOB. They are mapped to other KOODIs that already exist for other things.
Even this doesn’t work:
SELECT
...
CONCAT('123',
CASE
WHEN LEFT([PRODUCT],2) = 'PO' AND [LINE_OF_BUSINESS] = 'Q' THEN 'ABC'
ELSE '???'
END
) AS KOODI
CASE
WHEN [PRODUCT] IN ('AAA','BBB','CCC') THEN 'ABC'
WHEN LEFT([PRODUCT],2) = 'PO' AND [LINE_OF_BUSINESS] = 'Q' THEN 'ABC'
WHEN [PRODUCT] IN ('XXX','YYY','ZZZ') THEN 'XYZ'
ELSE '???'
END
) AS KOODI
That “OR” combined with “AND” in the same statement is bad syntax, to me.
UPDATE
I’m pretty sure it was a parenthesis problem…it wasn’t in my sample code but in my actual code.
If I clean that up I should be good.
Thanks for reading & thanks for your input, ideas, and help.
So I have records that have a date field and several other fields that can create a unique key. The dates are business dates, and I want to match records with 1 date to the business date immediately prior.
Because of the business date thing, i can’t use simple formulas like currdate -1 = prior date
I have considered building a dates table that has curr date and prior date on the same record and matching across that table.
Another way would be to join on priordate < curr date and select records with the max of priordate, but that seems like a bunch of matches for no purpose than to discard them.
Anyone have solutions they have used in the past and would like to share?
You could build a table with two columns - business date and the rank of the business date (from earliest to latest). The previous business day will just be the row where the rank = x - 1 (where x = the current days rank).
I thought of this, but didn’t know if there was a SQL function that assigned the rank. That left me thinking, build a table with the date and rank (somehow). I realized if I did this, I could then build a table with curr date and prior date, and skip the rank entirely.