sqlpp11 icon indicating copy to clipboard operation
sqlpp11 copied to clipboard

Using case in select statement.

Open slepasteur opened this issue 10 years ago • 6 comments

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

slepasteur avatar Dec 18 '15 12:12 slepasteur

Today this is not possible, but it is on my personal wish list. It should not be that hard to do.

rbock avatar Dec 18 '15 12:12 rbock

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.

slepasteur avatar Dec 18 '15 13:12 slepasteur

Hi,

please checkout the develop branch:

case_when(condition).then(A).else_(B)

Please note:

  • there has to be exactly one condition, one then, one else, i.e. there cannot be several when/then combinations, and the else_ is not optional
  • there is no case(variable).when(value).then...

Let me know if it works for you.

Best,

Roland

rbock avatar Dec 23 '15 15:12 rbock

case_when(condition).then(expression).else_(expression) is now supported by release-0.35

rbock avatar Dec 29 '15 10:12 rbock

@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?

margaretselzer avatar Sep 12 '24 15:09 margaretselzer

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.

rbock avatar Sep 13 '24 11:09 rbock