Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8360569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:30:04+00:00 2026-06-09T11:30:04+00:00

Help please, I have a table like this: | ID | userId | amount

  • 0

Help please, I have a table like this:

 | ID | userId   | amount  | type    |
 -------------------------------------
 |  1 |       10 |  10     | expense |
 |  2 |       10 |  22     | income  |
 |  3 |        3 |  25     | expense |
 |  4 |        3 |  40     | expense |
 |  5 |        3 |  63     | income  |

I’m looking for a way to use one query and retrive the balance of each user.

The hard part comes when the amounts has to be added on expenses and substracted on incomes.

This would be the result table:

 | userId | balance |
 --------------------
 |   10   |  12     |
 |    3   |  -2     |
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T11:30:05+00:00Added an answer on June 9, 2026 at 11:30 am

    This is easiest to do with a single group by:

    select user_id,
           sum(case when type = 'income' then amount else - amount end) as balance
    from t
    group by user_id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please help me to write the following sql. I have a table like this,
Please help me with this: I have a table like: id_feature id_product id_feature_value 1
may be too simple groovy question but....please help i have a list like this:
Please help me solve this problem with capybara I have a button like this
Need a query help. I have a table where data is represented like this
Please help! I have been grappling with this error for days and I cannot
Can anybody please help me with this as I have no idea why public
Please help this beginner here... I have a SQL Server 2008 R2 running on
i have a table like this: number stuff1 stuff2 stuff3 ------------------------------- 1 x y
I have a table called Periods that looks like this PeriodID | PeriodYear |

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.