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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T01:17:53+00:00 2026-06-19T01:17:53+00:00

Suppose I have a table like this: id | dates | customer | sale

  • 0

Suppose I have a table like this:

id | dates | customer | sale

And want to select, for each customer:

  • number of purchases
  • last purchase value
  • total amount of purchases, when it has a positive value

With this table definition:

CREATE TABLE sales (
    id int auto_increment primary key, 
    dates date,
    customer int,
    sale int
);

And this data:

INSERT INTO sales
    (dates, customer, sale)
VALUES
    ('2012-01-01', 1, 2),
    ('2012-02-01', 1, 8),
    ('2012-03-01', 2, 1),
    ('2012-04-01', 2, 7),
    ('2012-05-01', 2, -5),
    ('2012-06-01', 1, 5)

My results should be:

customer | sales | last_sale
1           3        5
2           3        7

I cannot figure out how to get it. I have reached this:

SELECT s.customer, COUNT(s.sale) total_sales, last_sale FROM sales AS s
JOIN
  (SELECT customer, sale last_sale FROM sales GROUP BY customer ORDER BY dates DESC) AS t
  ON t.customer=s.customer
GROUP BY s.customer

But it is not working. Any ideas of how to get this data?

All code is in SQL fiddle

  • 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-19T01:17:55+00:00Added an answer on June 19, 2026 at 1:17 am

    Try this

    SELECT a.customer, count(a.sale), max_sale
    FROM sales a
    INNER JOIN (SELECT customer, sale max_sale 
                from sales x where dates = (select max(dates) 
                                            from sales y 
                                            where x.customer = y.customer
                                            and y.sale > 0
                                           )
    
               )b
    ON a.customer = b.customer
    GROUP BY a.customer, max_sale;
    

    SQL DEMO

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

Sidebar

Related Questions

Suppose you have a table like this: id user score It's a dump of
A newbie question,really. Suppose I have a html table like this: <div id=div1> <table
Suppose I have the code like this: <table> <tr> <th>name</td> <th>price</td> </tr> <tr> <td>a</td>
Suppose I have a table, Foo, that looks like this: ID | Name |
Suppose I have a table, Document, that looks something like this: _______________________ | Document
Suppose I have a table like this: | uid | group_id | ----------------- |
Suppose I have a table, TeamRatings, that looks something like this |---Team----|--ValuationDate--|-Rating-| |--Saints---|---10/15/2012----|---81.1-| |--Broncos--|---10/15/2012----|---91.1-|
Suppose I have a table like: create table { id numeric(5,3), code varchar(10) }
Suppose I have a table of users. Something like: ID integer, USER text, POSITION
I have requirement like, suppose I have a 'property' table which has 'ListingKey' field

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.