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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:18:26+00:00 2026-06-17T18:18:26+00:00

I have a not-very-normalized MySQL table with the following values: customer_ID | date |

  • 0

I have a not-very-normalized MySQL table with the following values:

customer_ID | date | score | sale

As I am scoring the “purchase experience”, I would like to check the total sale value for each customer based on his last score. I do not mind about previous scores, just about last one.

Let’s say I have:

customer_ID | date      | score | sale  
a           | yesterday | 3     | 10  
a           | today     | 6     | 35  
b           | today     | 10    | 20  
c           | yesterday | 4     | 5  

The result for customers purchases with score > 5 should be:

num of customers | total sale values  
2                | 65

Any idea of how to create such query?

Edit:

What I want to know is how much has a customer spent in purchases in total, but just on customers whose last score was bigger than 5.

  • 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-17T18:18:27+00:00Added an answer on June 17, 2026 at 6:18 pm
    SELECT  COUNT(DISTINCT aa.customer_ID) `num of customers`,
            SUM(aa.sale) `total sale values `
    FROM    table1 aa
            INNER JOIN
            (
                SELECT  a.customer_ID
                FROM    table1  a
                        INNER JOIN
                        (
                            SELECT  customer_ID, max(date) max_date
                            FROM    table1
                            GROUP   BY customer_ID
                        ) b ON a.customer_ID = b.customer_ID AND
                                a.date = b.max_date AND a.score > 5
            ) final ON aa.customer_ID = final.customer_ID
    
    • SQLFiddle Demo

    much more simplified,

    SELECT  COUNT(DISTINCT c.customer_ID) `num of customers`,
            SUM(c.sale) `total sale values `
    FROM    table1  a
            INNER JOIN
            (
                SELECT  customer_ID, max(date) max_date
                FROM    table1
                GROUP   BY customer_ID
            ) b ON a.customer_ID = b.customer_ID AND
                    a.date = b.max_date AND a.score > 5
            INNER JOIN table1 c
                ON a.customer_ID = c.customer_ID
    
    • SQLFiddle Demo
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I have some experience with programming but I'm not very good with pointers.
I have one very large, un-normalized table which I am in the process of
I have not used jquery much yet and not very familiar with it. Trying
I have not worked with parameters in ADO.Net very much. I'm writing a custom
I'm not very good at PHP and would like to have a PHP function
I'm not very good in SQL and HQL... I have two domains: class Hotel
I'm not very expert with Perl, so I preferred to ask you. I have
I'll admit I'm not very adept at key verification. What I have is a
The title is not very clear, so I'll try to explain better. I have
I have to display current server host (in PHP, but it is not very

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.