Query with case condition
See original GitHub issueHey, couldn’t find an answer for this in the docs or anywhere online - is there a way to build a query with conditions? Something such as -
SELECT CASE WHEN a = 1
THEN 1
ELSE 0
END as test123
?
And if that’s possible, can something a bit more complex like this be done?
SELECT SUM(CASE WHEN a = 1
THEN 1
ELSE 0 END) AS test123
Thanks
Issue Analytics
- State:
- Created 7 years ago
- Reactions:1
- Comments:5 (3 by maintainers)
Top Results From Across the Web
SQL CASE Expression - W3Schools
The SQL CASE Expression. The CASE expression goes through conditions and returns a value when the first condition is met (like an if-then-else...
Read more >Querying data using the SQL Case statement
The Case statement in SQL is mostly used in a case with equality expressions. The SQL Case statement is usually inside of a...
Read more >SQL CASE | Intermediate SQL - Mode Analytics
The CASE statement is SQL's way of handling if/then logic. The CASE statement is followed by at least one pair of WHEN and...
Read more >Case Statement in SQL – Example Query - freeCodeCamp
If you need to add a value to a cell conditionally based on other cells, SQL's case statement is what you'll use.
Read more >Understanding the SQL CASE Statement and its many uses
In this type of CASE statement, we use equality check expressions. The following query implements a simple CASE expression.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Sounds kind of interesting. There has been also some other needs for building other kind of expressions which are just part of the query. This would fit nicely to that kind of expression builder.
If there is still a need for this type of functionality, here is an implementation that I would be interested in PR’ing here. https://www.npmjs.com/package/knex-case
Let me know if anyone has any opinions on this!