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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:13:28+00:00 2026-05-14T04:13:28+00:00

Here is my data structure alt text http://luvboy.co.cc/images/db.JPG when i try this sql select

  • 0

Here is my data structure

alt text http://luvboy.co.cc/images/db.JPG

when i try this sql

select rec_id, customer_id, dc_number, balance
from payments
where customer_id='IHS050018'
group by dc_number
order by rec_id desc;

something is wrong somewhere, idk

I need

rec_id  customer_id   dc_number   balance

2        IHS050018      DC3        -1
3        IHS050018      52         600 

I want the recent balance of the customer with respective to dc_number ?

Thanx

  • 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-14T04:13:28+00:00Added an answer on May 14, 2026 at 4:13 am

    There are essentially two ways to get this

    select p.rec_id, p.customer_id, p.dc_number, p.balance
    from payments p
    where p.rec_id IN (
        select s.rec_id
        from payments s
        where s.customer_id='IHS050018' and s.dc_number = p.dc_number
        order by s.rec_id desc
        limit 1);
    

    Also if you want to get the last balance for each customer you might do

    select p.rec_id, p.customer_id, p.dc_number, p.balance
    from payments p
    where p.rec_id IN (
        select s.rec_id
        from payments s
        where s.customer_id=p.customer_id and s.dc_number = p.dc_number
        order by s.rec_id desc
        limit 1);
    

    What I consider essentially another way is utilizing the fact that select rec_id with order by desc and limit 1 is equivalent to select max(rec_id) with appropriate group by, in full:

    select p.rec_id, p.customer_id, p.dc_number, p.balance
    from payments p
    where p.rec_id IN (
        select max(s.rec_id)
        from payments s
        group by s.customer_id, s.dc_number
        );
    

    This should be faster (if you want the last balance for every customer), since max is normally less expensive then sort (with indexes it might be the same).

    Also when written like this the subquery is not correlated (it need not be run for every row of the outer query) which means it will be run only once and the whole query can be rewritten as a join.

    Also notice that it might be beneficial to write it as correlated query (by adding where s.customer_id = p.customer_id and s.dc_number = p.dc_number in inner query) depending on the selectivity of the outer query.

    This might improve performance, if you look for the last balance of only one or few rows.

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

Sidebar

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.