How to use 'sum' expression with 'case-when'
See original GitHub issueI 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:
- Created 2 years ago
- Comments:7
Top 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 >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 FreeTop 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
Top GitHub Comments
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 잘 사용해주셔서 기쁩니다. 써보시니 어떠신가요? 아직 실제 유저의 피드백을 받지 못해 궁금합니다.
Thank you so much. It works well 😃