how to put nested case-when condition in postgresql query
CASE … WHEN … END is an expression. It can be nested like any other expression. CASE WHEN condition THEN CASE WHEN othercondition THEN …. END END
CASE … WHEN … END is an expression. It can be nested like any other expression. CASE WHEN condition THEN CASE WHEN othercondition THEN …. END END
The value that is returned will be the value of the THEN expression for the earliest WHEN clause (textually) that matches. That does mean that if your line 2 conditions are met, the result will be A2. But, if your THEN expressions were more complex than just literal values, some of the work to evaluate … Read more
CASE AlarmEventTransactions.DeviceID should just be CASE. You are mixing the 2 forms of the CASE expression.
The problem is that the CASE statement won’t work in the way you’re trying to use it. You can only use it to switch the value of one field in a query. If I understand what you’re trying to do, you might need this: SELECT ActivityID, FieldName = CASE WHEN ActivityTypeID <> 2 THEN (Some … Read more
SELECT CASE WHEN xyz.something = 1 THEN ‘SOMETEXT’ WHEN xyz.somethingelse = 1 THEN ‘SOMEOTHERTEXT’ WHEN xyz.somethingelseagain = 2 THEN ‘SOMEOTHERTEXTGOESHERE’ ELSE ‘SOMETHING UNKNOWN’ END AS ColumnName;
Use ifelse df %>% mutate(g = ifelse(a == 2 | a == 5 | a == 7 | (a == 1 & b == 4), 2, ifelse(a == 0 | a == 1 | a == 4 | a == 3 | c == 4, 3, NA))) Added – if_else: Note that in dplyr 0.5 … Read more
That format requires you to use either: CASE ebv.db_no WHEN 22978 THEN ‘WECS 9500’ WHEN 23218 THEN ‘WECS 9500’ WHEN 23219 THEN ‘WECS 9500’ ELSE ‘WECS 9520’ END as wecs_system Otherwise, use: CASE WHEN ebv.db_no IN (22978, 23218, 23219) THEN ‘WECS 9500’ ELSE ‘WECS 9520’ END as wecs_system