Skip to content
Advertisement

Getting date, and count of unique customers when first order was placed

I have a table called orders that looks like this:

And that contains the following data:

I would like a result set returning each unique date, and a separate column showing how many customers that placed their first order that day.

I’m having problems with querying this the right way, especially when the data consists of multiple orders the same day from the same customer.

My approach has been to

  1. Get all unique memberids that placed an order during the time period I want to look at
  2. Filter out the ones that placed their first order during the period by comparing the memberids that has placed an order before the timeperiod
  3. Grouping by delivery date, and counting all unique memberids (but this obviously counts unique memberids each day individually!)

Here’s the corresponding SQL:

But this results in the following with the above data:

The count for 2019-10-26 should be 1.

Appreciate any help 🙂

Advertisement

Answer

You can aggregate twice:

The subquery gives you the first order data of each member, then the outer query aggregates and counts by first order date.

This demo on DB Fiddle with your sample data returns:

first_deliverydate | cnt
:----------------- | --:
2019-10-21         |   1
2019-10-24         |   1
2019-10-25         |   2
2019-10-26         |   1

In MySQL 8.0, This can also be achieved with window functions:

Demo on DB Fiddle

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement