请求有两个不同的日期范围

时间:2017-08-30 15:32:22

标签: mysql sql select inner-join

我在使用两个不同的日期范围发出请求时遇到了一个小问题。

我需要 nb_sales last_sales 直到2014年,但频率仅限过去一年。

我想要的结果:

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?

第1-3栏属于第一个日期范围:今天至2014年 第4列处于一个附加日期范围:今天到y-1

所以我试着:

  1. 创建临时表并插入频率
  2. SELECT customer_id nb_sales last_sales 频率LEFT OUTER JOIN < / LI>

    第一步还可以,但对于第二步,我没有任何结果或错误消息......当我想{临时表LEFT OUTER JOIN时,就会发生这种情况:

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

    也许你有更好的主意?

    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 ;
    

1 个答案:

答案 0 :(得分:0)

我终于找到了解决方案 非常感谢你的帮助:))

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;