Search code examples
mysqlsqlselectinner-join

Request with two different dates ranges


I've a little issue with MySQL for making a request with two different dates ranges.

I need to have nb_sales and last_sales until 2014 but frequence only for the past year.

The result I want :

customer_id     |    nb_sales    |   last_sales  |   frequence
---------------------------------------------------------------
Customer ID     | Sales make by  |  How many days|  How many sales
                |  the customer  |  since the    |  has been made
                |                |  last sales?  |  this year?

Column 1-3 are in the first date range : today to 2014
Column 4 is in a seconde date range : today to y-1

So I tried to :

  1. Create a temporary table and insert frequence
  2. SELECT customer_id, nb_sales, last_sales and frequence with LEFT OUTER JOIN

The first step is ok but for the second one I don't have any result or error message... And this happened when I wanted to LEFT OUTER JOIN my temporary table:

LEFT OUTER JOIN tmp_frequence 
  ON tmp_frequence.client_id = sales_flat_order.customer_id

Maybe you have a better idea?

CREATE TEMPORARY TABLE tmp_frequence (
    id INT UNSIGNED NOT NULL AUTO_INCREMENT,
    client_id INT,
    frequence INT,
    PRIMARY KEY (id)
);

INSERT INTO tmp_frequence (client_id, frequence)
SELECT sales_flat_order.customer_id, COUNT(sales_flat_order.entity_id)
FROM sales_flat_order
WHERE sales_flat_order.created_at BETWEEN '2014-05-22 00:00:00' and '2017-07-31 23:59:59'
GROUP BY sales_flat_order.customer_id;

/* ------------------------------  */ 
SELECT  
-- * , 
sales_flat_order.customer_id customer_id,
COUNT(sales_flat_order.entity_id) nb_sales,
DATEDIFF("2017-07-31",DATE_FORMAT(MAX(sales_flat_order_item.created_at),"%Y-%m-%d")) last_sales,
tmp_frequence.frequence frequence

FROM adl_ec.sales_flat_order_item 
LEFT OUTER JOIN sales_flat_order 
  ON sales_flat_order.entity_id = sales_flat_order_item.order_id 
LEFT OUTER JOIN tmp_frequence 
  ON tmp_frequence.client_id=sales_flat_order.customer_id

WHERE sales_flat_order_item.created_at BETWEEN '2014-05-22 00:00:00' and '2017-07-31 23:59:59'
GROUP BY customer_id;

DROP TABLE tmp_frequence ;

Solution

  • I've Finally found a solution.
    Thank you really for your help :)

    DROP TABLE IF EXISTS tmp_frequence;
    CREATE TEMPORARY TABLE tmp_frequence (
        id INT UNSIGNED NOT NULL AUTO_INCREMENT,
        client_id INT,   
        recence INT,
        frequence INT,
        montant INT,
        PRIMARY KEY (id)
    );
    
    INSERT INTO tmp_frequence (client_id, frequence)
    SELECT sales_flat_order.customer_id, COUNT(sales_flat_order.entity_id)
    FROM sales_flat_order
    WHERE sales_flat_order.created_at BETWEEN '2016-07-31 00:00:00' and '2017-07-31 23:59:59'
    GROUP BY sales_flat_order.customer_id;
    
    
    INSERT INTO tmp_frequence (client_id, recence, montant)
    SELECT
    sales_flat_order.customer_id,
    DATEDIFF("2017-07-31",DATE_FORMAT(MAX(sales_flat_order_item.created_at),"%Y-%m-%d")) recence,
    COUNT(sales_flat_order.grand_total) montant
    
    FROM adl_ec.sales_flat_order_item 
    LEFT OUTER JOIN sales_flat_order ON sales_flat_order.entity_id = sales_flat_order_item.order_id 
    AND sales_flat_order_item.created_at BETWEEN '2014-05-22 00:00:00' and '2017-07-31 23:59:59'
    AND qty_invoiced >0
    AND sales_flat_order_item.sku NOT LIKE '%abo%'
    AND sales_flat_order.total_qty_ordered < 5
    
    GROUP BY customer_id;
    
    SELECT tmp_frequence.client_id,MAX(tmp_frequence.recence) Recence,MAX(tmp_frequence.frequence) Frequence,MAX(tmp_frequence.montant) Montant
    FROM tmp_frequence 
    GROUP BY tmp_frequence.client_id;