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 7650021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:08:43+00:00 2026-05-31T11:08:43+00:00

I have a MySQL table of billing records. Each entry records either a payment

  • 0

I have a MySQL table of billing records. Each entry records either a payment or a charge, the amount, the date applied, and the related customer id. If there are more charges than payments, how can I get the outstanding charge records sorted by most recent, and if there are more payments than charges, the payment credits sorted by most recent? Basically, if I have 3 rows of charges amounting to $1.25 each, and 2 rows of $1.50, how can I get the 3 charge record that is the most recent, and know that there is only $0,75 left due? I hope that makes sense.

Let’s say we have this table:

user_id | amount | date_applied | type
---------------------------------------
    1       1.25    *datetime*      -
    1       1.25    *datetime*      -
    1       1.25    *datetime*      -
    1       1.50    *datetime*      +
    1       1.50    *datetime*      +

the result should be something similar to this:

user_id | amount | date_applied | type
---------------------------------------
    1       0.75    *datetime*      -

I may not even be thinking about this right. I am open to other suggestions

  • 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-05-31T11:08:44+00:00Added an answer on May 31, 2026 at 11:08 am

    Your question is a bit tricky. That’s because there is no clear definition on what datetime to show. It seems you want to have the sum of all positive and negative values on the table per user. The problem is that when you make a total you can’t have a detail of the datetimes. You will have to choose one of them, but there won’t be any order in the result as there will only be one datetime per user in the result (in your example you can see for user 1 there is only one datetime).

    Also, do you need the type column? Why not add it to the amount?

    This is the closest I can get to your needs given the detail provided:

    select user_id, sum(if(type = '+', amount, -amount)) amount from t
    group by user_id
    
    +---------+--------+
    | USER_ID | AMOUNT |
    +---------+--------+
    |       1 | -0.75  |
    +---------+--------+
    

    Note: If you really need the datetime put a real date for each record and update the expected result with the expected date value.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a MySQL table called items that contains thousands of records. Each record
I have a MYSQL table with 3 fields: CODE, DATE, COUNT. CODE is the
I have a MySql table named 'comments' : id | date | movie_id |
I have a mySQL table with records of users who have visited my site.
Ok, I have 2 million rows. Each has a billing date. i'm trying to
I have mysql table with collumns like 'operation.date', 'operation.name' and etc. After fetching that
I have a mysql table which will store users email addresses (each is unique
I have mysql table with the following structure. All records are static and only
I have mysql table Stop_Times with 20 Million records. I am using MyISAM storage
I have mysql table that has a column that stores xml as a string.

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.