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

  • SEARCH
  • Home
  • 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 6024451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:09:26+00:00 2026-05-23T04:09:26+00:00

Considering a table customerBalance with 3 columns: name, date and balance. Suppose a set

  • 0

Considering a table customerBalance with 3 columns: name, date and balance. Suppose a set of records like:

cus_name    cus_date    cus_balance
John        06/14/2011  1000
John        06/15/2011   500
John        06/16/2011     0
Mary        06/14/2011  3000
Mary        06/15/2011  2800
Mary        06/16/2011     0

How to create a SQL query which returns, for the date 6/16/2011 instead 0, the last non-zero value based on date (in sample, $500 for John and $2800 for Mary)?

I’m trying to do it using a subquery which uses Max function to retrieve the last date with non-zero value, but I didn’t succeed. This example is quite “nonsensical”, but I really need to do an operation like this in my dataset. Thanks!

  • 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-23T04:09:27+00:00Added an answer on May 23, 2026 at 4:09 am

    Note: If you can specify the DB and version this query can be improved.

    Try this:

    SELECT *
      FROM customers
     WHERE (cus_name, cus_date) 
            IN 
            (
                SELECT cus_name, MAX(cus_date)
                  FROM customers
                 WHERE cus_balance <> 0
                 GROUP BY cus_name
            )
    

    Update: Alternate version:

    SELECT a.*
        FROM customers a,
                (
                    SELECT cus_name, MAX(cus_date)
                        FROM customers
                     WHERE cus_balance <> 0
                     GROUP BY cus_name
                ) b
     WHERE a.cus_name = b.cus_name
       AND a.cus_date = b.cus_date
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm considering dropping an index from a table in a SQL Server 2005 instance.
Considering you have an MVVM Architecture in WPF like Josh Smith's examples How would
I want to add two columns values of my table and sort it in
Considering the following table: someId INTEGER #PK ageStart TINYINT(3) ageEnd TINYINT(3) dateBegin INTEGER dateEnd
Currently we're using INT(21)* for all autoincrement id columns in out 30+ table database.
Considering this table: create table x (id int, total int, diff int) And this
I am considering moving my MyISAM table to InnoDB. I have a lot of
I am considering two options for table design and I'm not sure what the
SHORT: my python code generates a webpage with a table. i'm considering rewriting it
I have a table in Mysql (firstname,lastname,data1,data2,...) that one field name is MYDATE and

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.