Using case in select statement.
Is it possible to perform requests with case statements using sqlpp11?
SELECT CASE WHEN col < 100
THEN col
ELSE othercol
END AS colname
FROM table
Today this is not possible, but it is on my personal wish list. It should not be that hard to do.
Ok I will do two requests for now and do the test in the code. Let me know if I can be of any help.
Hi,
please checkout the develop branch:
case_when(condition).then(A).else_(B)
Please note:
- there has to be exactly one
condition, onethen, oneelse, i.e. there cannot be severalwhen/thencombinations, and theelse_is not optional - there is no
case(variable).when(value).then...
Let me know if it works for you.
Best,
Roland
case_when(condition).then(expression).else_(expression) is now supported by release-0.35
@rbock, I was wondering... Would it be possible to implement case_when in a more flexible way similarly to how a select statement can be built up or the joins can be chained one after the other? I'm guessing the challenge is that there has to be an END at the end of the case. But what if there was a structure like
case_begin()
.when(condition_1).then(value_1)
.when(condition_2).then(value_2)
...
.else(value_else)
.case_end()
WDYT?
Yes, that would be possible. Feel free to open a new feature request.
I would first complete the work on the optional branch (progressing fine, but will take while to get it done). And then upgrade case statements to allow for multiple branches.