question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Query with case condition

See original GitHub issue

Hey, 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:open
  • Created 7 years ago
  • Reactions:1
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
elhigucommented, May 31, 2018

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.

4reactions
ericadamskicommented, May 31, 2018

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!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found