Answered by:
HOW TO USE SUM FUNCTION INSIDE A SELECT

Question
-
Hello,
I am trying to sum the debits columns and credit columns so I wrote: but is not working I dont get the debit column
select C.code_id,C.AMOUNT,C.BALANCE_AMOUNT,
(SELECT (case when C.BALANCE_AMOUNT > 0 then C.BALANCE_AMOUNT else 0 end) as debits )
from charge C
group by C.code_id, C.AMOUNT, C.BALANCE_AMOUNT
- Edited by Ysabel1111 Wednesday, August 5, 2020 3:40 AM
Wednesday, August 5, 2020 3:38 AM
Answers
-
Can you please provide input (as create table and insert statements) and the desired output? I can not figure from the query you posted what do you actually want.
Most likely your query needs to be
select C.code_id, C.AMOUNT, SUM (case when C.BALANCE_AMOUNT < 0 then C.BALANCE_AMOUNT else 0 end) as Credits, SUM (case when C.BALANCE_AMOUNT > 0 then C.BALANCE_AMOUNT else 0 end) as Debits from charge C group by C.code_id, C.AMOUNT
For every expert, there is an equal and opposite expert. - Becker's Law
My blog
My TechNet articles
- Edited by Naomi N Wednesday, August 5, 2020 4:49 AM
- Marked as answer by Ysabel1111 Wednesday, August 5, 2020 12:49 PM
Wednesday, August 5, 2020 4:47 AM -
Hi Ysabel1111
Could you please share us your table structure (CREATE TABLE …) and some sample data(INSERT INTO …)
along with your expected result? So that we’ll get a right direction and make some test.Please try :
select C.code_id,C.AMOUNT,C.BALANCE_AMOUNT, sum(case when C.BALANCE_AMOUNT > 0 then C.BALANCE_AMOUNT else 0 end) as debits from charge C group by C.code_id, C.AMOUNT, C.BALANCE_AMOUNT
Best Regards
Echo
""SQL Server related"" forum will be migrated to a new home on Microsoft Q&A SQL Server!
We invite you to post new questions in the "SQL Server related" forum’s new home on Microsoft Q&A SQL Server !
For more information, please refer to the sticky post.
- Edited by Echo Liuz Wednesday, August 5, 2020 6:00 AM
- Marked as answer by Ysabel1111 Wednesday, August 5, 2020 12:49 PM
Wednesday, August 5, 2020 5:30 AM
All replies
-
Can you please provide input (as create table and insert statements) and the desired output? I can not figure from the query you posted what do you actually want.
Most likely your query needs to be
select C.code_id, C.AMOUNT, SUM (case when C.BALANCE_AMOUNT < 0 then C.BALANCE_AMOUNT else 0 end) as Credits, SUM (case when C.BALANCE_AMOUNT > 0 then C.BALANCE_AMOUNT else 0 end) as Debits from charge C group by C.code_id, C.AMOUNT
For every expert, there is an equal and opposite expert. - Becker's Law
My blog
My TechNet articles
- Edited by Naomi N Wednesday, August 5, 2020 4:49 AM
- Marked as answer by Ysabel1111 Wednesday, August 5, 2020 12:49 PM
Wednesday, August 5, 2020 4:47 AM -
Hi Ysabel1111
Could you please share us your table structure (CREATE TABLE …) and some sample data(INSERT INTO …)
along with your expected result? So that we’ll get a right direction and make some test.Please try :
select C.code_id,C.AMOUNT,C.BALANCE_AMOUNT, sum(case when C.BALANCE_AMOUNT > 0 then C.BALANCE_AMOUNT else 0 end) as debits from charge C group by C.code_id, C.AMOUNT, C.BALANCE_AMOUNT
Best Regards
Echo
""SQL Server related"" forum will be migrated to a new home on Microsoft Q&A SQL Server!
We invite you to post new questions in the "SQL Server related" forum’s new home on Microsoft Q&A SQL Server !
For more information, please refer to the sticky post.
- Edited by Echo Liuz Wednesday, August 5, 2020 6:00 AM
- Marked as answer by Ysabel1111 Wednesday, August 5, 2020 12:49 PM
Wednesday, August 5, 2020 5:30 AM -
Thank you it works!!Wednesday, August 5, 2020 12:49 PM