Skip to content
Advertisement

Count date if it is in the same months as the first order of the customer

DB Fiddle

Expected Result


My database consists of two tables returned and customers.
In the customers table you can find the date of the first_order per customer.
In the returned table you can find each return a customer has done so far.

Now, I want to create a list and count all returned_dates which are in the same month as the first_order date of a certain customer.
If there are multiple orders in the same month then only the first match should be counted 1.
The others should be 0. (see Customer_01 in the example)


The difference to this question is that there is no return_date that matches exactly with a first_order date.
Therefore, this query does not work:

Do you have any idea how I can achieve the expected result?

Advertisement

Answer

You seem to want:

This flags the first return as “1” only if it is the first order and occurs within a month of the start date for the customer.

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