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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:58:49+00:00 2026-05-22T22:58:49+00:00

Having trouble with a query to return the newest order of any grouped set

  • 0

Having trouble with a query to return the newest order of any grouped set of orders having more than 1 order. CREATE & INSERTs for the test data are below.

This query returns the unique customer id’s I want to work with, along with the grouped order_id’s. Of these records, I only need the most recent order (based on date_added).

SELECT COUNT(customer_id), customer_id, GROUP_CONCAT(order_id) FROM orderTable GROUP BY customer_id HAVING COUNT(customer_id)>1 LIMIT 10;

mysql> SELECT COUNT(customer_id), customer_id, GROUP_CONCAT(order_id) FROM orderTable     GROUP BY customer_id HAVING COUNT(customer_id)>1 LIMIT 10;
+--------------------+-------------+------------------------+
| COUNT(customer_id) | customer_id | GROUP_CONCAT(order_id) |
+--------------------+-------------+------------------------+
|                  2 | 0487        | F9,Z33                 |
|                  3 | 1234        | 3A,5A,88B              |
+--------------------+-------------+------------------------+
2 rows in set (0.00 sec)

I’m looking for order Z33 (customer_id 0487) and 3A (customer_id 1234).

For clarification, I do not want orders for customers that have only ordered once.

Any help or tips to get me pointed in the right direction appreciated.

Sample table data:

—

— Table structure for table orderTable

CREATE TABLE IF NOT EXISTS orderTable (
customer_id varchar(10) NOT NULL,
order_id varchar(4) NOT NULL,
date_added date NOT NULL,
PRIMARY KEY (customer_id,order_id)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

—

— Dumping data for table orderTable

INSERT INTO orderTable (customer_id, order_id, date_added) VALUES
(‘1234’, ‘5A’, ‘1997-01-22’),
(‘1234′, ’88B’, ‘1992-05-09’),
(‘0487’, ‘F9’, ‘2002-01-23’),
(‘5799’, ‘A12F’, ‘2007-01-23’),
(‘1234’, ‘3A’, ‘2009-01-22’),
(‘3333’, ‘7FHS’, ‘2009-01-22’),
(‘0487’, ‘Z33’, ‘2004-06-23’);

==========================================================

  • 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-22T22:58:50+00:00Added an answer on May 22, 2026 at 10:58 pm

    Clarification of the query.
    The question was to only include those customers that had more… hence my query has it INSIDE with the GROUP BY… This way it ONLY GIVES the customer in question that HAD multiple orders, but at the same time, only gives the most recent date OF the last order for the person… Then the PreQuery is re-joined to the orders table by the common customer ID, but only for the order that matches the last date as detected in the prequery. If a customer only had a single order, its inner PreQuery count would have only been 1 and thus excluded from the final PreQuery result set.

    select ot.*
       from
          ( select
                customer_id,
                max( date_added ) as LastOrderDate,
             from
                orderTable
             having 
                count(*) > 1
             group by
                customer_id ) PreQuery
         join orderTable ot
            on PreQuery.Customer_ID = ot.Customer_ID
           and PreQuery.LastOrderDate = ot.date_added
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having some trouble building the query I need to return the AccountID
I'm having trouble finding a way to write a query that will return all
I'm having trouble crafting a query to return the correct data, and I'm becoming
I'm new to LINQ , and I'm having trouble organizing this query to return
I am having trouble getting my query to return unique results. users_systems db setup:
I am having trouble pulling a query where i want to return results that
I'm having trouble with an SQL query in a scope. I need to return
I am having trouble writing query so that I can query the content of
I'm having trouble building a query. I can do what I want in 3
I'm having trouble with a query in SQL Server 2008 on some forex trading

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.