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.

How to use 'sum' expression with 'case-when'

See original GitHub issue

I tried to convert below query to Kotlin-jdsl.

SELECT  id,
        SUM( CASE WHEN role IN ('A','B') THEN amount ELSE 0 END ) AS balance
FROM    TEST_TABLE
GROUP BY id
return query.listQuery {
    selectMulti(
        col(TestTable::id),
        sum(case(
          `when`(col(TestTable::role).`in`("A","B")).then(col(TestTable::occurAmount)),
          `else` = literal(0L)
        )),
    )
    from(entity(TestTable::class))
    groupBy(
        col(TestTable::id),
    )
}

But it could not possible because SumSpec might not support below syntax.

  sum(case(
    `when`(col(TestTable::role).`in`("A","B")).then(col(TestTable::occurAmount)),
    `else` = literal(0L)
  )),

Is there way to possible using sum expression with case-when ?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
cj848commented, Feb 17, 2022

I’m glad you enjoyed using it. How do you feel about using it? I’m curious because we haven’t received feedback from real users yet.

– korean 잘 사용해주셔서 기쁩니다. 써보시니 어떠신가요? 아직 실제 유저의 피드백을 받지 못해 궁금합니다.

1reaction
nayasiscommented, Feb 17, 2022

Thank you so much. It works well 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Use CASE WHEN With SUM() in SQL
Using a CASE WHEN expression to assign values 0 or 1 to the table rows is just a little trick to make SUM()...
Read more >
sql - SELECT query with CASE condition and SUM()
Use an "Or" Select SUM(CAmount) as PaymentAmount from TableOrderPayment where (CPaymentType='Check' Or CPaymentType='Cash') and CDate <= case CPaymentType ...
Read more >
How to Use SQL SUM Function: 5 Use Cases
Use Case 1: Simple SQL SUM Function. As stated above, I have a table named Employee with few columns. Assume that your company...
Read more >
SUMIF in SQL: SUM(CASE WHEN <condition> THEN ...
The case expression accepts different values in the when and then branches. This allows you to do the same thing as the third...
Read more >
13. CASE WHEN with SUM
Inside SUM , we put a CASE WHEN statement. When the value in the column scholarship is true, then we add the value...
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