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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:30:25+00:00 2026-05-18T05:30:25+00:00

I’m trying to write a query that can select a particular date and count

  • 0

I’m trying to write a query that can select a particular date and count how many of those customers have placed orders previously and how many are new. For simplicity, here is the table layout:

id (auto) | cust_id | purchase_date
-----------------------------------  
1         | 1       | 2010-11-15  
2         | 2       | 2010-11-15  
3         | 3       | 2010-11-14  
4         | 1       | 2010-11-13  
5         | 3       | 2010-11-12  

I was trying to select orders by a date and then join any previous orders on the same user_id from previous dates, then count how many had orders, vs how many didnt. This was my failed attempt:

SELECT SUM( 
CASE WHEN id IS NULL 
THEN 1 
ELSE 0 
END ) AS new, SUM( 
CASE WHEN id IS NOT NULL 
THEN 1 
ELSE 0 
END ) AS returning
FROM (

SELECT o1 . * 
FROM orders AS o
LEFT JOIN orders AS o1 ON ( o1.user_id = o.user_id
AND DATE( o1.created ) =  "2010-11-15" ) 
WHERE DATE( o.created ) <  "2010-11-15"
GROUP BY o.user_id
) AS t
  • 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-18T05:30:25+00:00Added an answer on May 18, 2026 at 5:30 am

    Given a reference data (2010-11-15), then we are interested in the number of distinct customers who placed an order on that date (A), and we are interested in how many of those have placed an order previously (B), and how many did not (C). And clearly, A = B + C.

    Q1: Count of orders placed on reference date

    SELECT COUNT(DISTINCT Cust_ID)
      FROM Orders
     WHERE Purchase_Date = '2010-11-15';
    

    Q2: List of customers placing order on reference date

    SELECT DISTINCT Cust_ID
      FROM Orders
     WHERE Purchase_Date = '2010-11-15';
    

    Q3: List of customers who placed an order on reference date who had ordered before

    SELECT DISTINCT o1.Cust_ID
      FROM Orders AS o1
      JOIN (SELECT DISTINCT o2.Cust_ID
              FROM Orders AS o2
             WHERE o2.Purchase_Date = '2010-11-15') AS c1
        ON o1.Cust_ID = c1.Cust_ID
     WHERE o1.Purchase_Date < '2010-11-15';
    

    Q4: Count of customers who placed an order on reference data who had ordered before

    SELECT COUNT(DISTINCT o1.Cust_ID)
      FROM Orders AS o1
      JOIN (SELECT DISTINCT o2.Cust_ID
              FROM Orders AS o2
             WHERE o2.Purchase_Date = '2010-11-15') AS c1
        ON o1.Cust_ID = c1.Cust_ID
     WHERE o1.Purchase_Date < '2010-11-15';
    

    Q5: Combining Q1 and Q4

    There are several ways to do the combining. One is to use Q1 and Q4 as (complicated) expressions in the select-list; another is to use them as tables in the FROM clause which don’t need a join between them because each is a single-row, single-column table that can be joined in a Cartesian product. Another would be a UNION, where each row is tagged with what it calculates.

    SELECT (SELECT COUNT(DISTINCT Cust_ID)
              FROM Orders
             WHERE Purchase_Date = '2010-11-15') AS Total_Customers,
           (SELECT COUNT(DISTINCT o1.Cust_ID)
              FROM Orders AS o1
              JOIN (SELECT DISTINCT o2.Cust_ID
                      FROM Orders AS o2
                     WHERE o2.Purchase_Date = '2010-11-15') AS c1
                ON o1.Cust_ID = c1.Cust_ID
             WHERE o1.Purchase_Date < '2010-11-15') AS Returning_Customers
      FROM Dual;
    

    (I’m blithely assuming MySQL has a DUAL table – similar to Oracle’s. If not, it is trivial to create a table with a single column containing a single row of data. Update 2: bashing the MySQL 5.5 Manual shows that ‘FROM Dual’ is supported but not needed; MySQL is happy without a FROM clause.)

    Update 1: added qualifier ‘o1.Cust_ID’ in key locations to avoid ‘ambiguous column name’ as indicated in the comment.

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

Sidebar

Related Questions

I am trying to loop through a bunch of documents I have to put
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.