2017-03-07 62 views
1

我有兩張桌子 - 產品和價格如何獲得與上個月相比最大的差異?

每個月的表格每個產品的價格都會包含新的價格。我怎樣才能得到5種價格與上個月價格差距最大的產品?

表產品

id | name 
1 | apples 
2 | pears 
3 | bananas 

表價格

id | price | product_id | created_at 
1 | 10 |  1  | 2017-02-07 07:00:00 
2 | 10 |  2  | 2017-02-07 07:00:00 
3 | 15 |  3  | 2017-02-07 07:00:00 
5 | 15 |  1  | 2017-03-07 07:00:00 
6 | 20 |  2  | 2017-03-07 07:00:00 
7 | 25 |  3  | 2017-03-07 07:00:00 

其結果將是找出

1. Bananas has prices by 15 higher (lastMonth: 15, now: 25) 
2. Pears 2 has prices by 10 higher (lastMonth: 10, now: 20) 
3. Apples has prices by 5 higher (lastMonth: 10, now: 15) 

我在想這樣的事情(UFF我知道這是可怕的)

SELECT products.id, products.name, prices.beforePrice, prices.afterPrice, prices.difference 
FROM products 
INNER JOIN prices ON products.id = prices.product_id 
WHERE 
    ( 
     SELECT *biggest-difference* 
     FROM prices 
     WHERE *difference_between_last_2_months* 
     GROUP BY product_id 
     LIMIT 5 
    ) 

回答

2

創建表/插入數據

CREATE TABLE Products 
    (`id` INT, `name` VARCHAR(7)) 
; 

INSERT INTO Products 
    (`id`, `name`) 
VALUES 
    (1, 'apples'), 
    (2, 'pears'), 
    (3, 'bananas') 
; 


CREATE TABLE Prices 
    (`id` INT, `price` INT, `product_id` INT, `created_at` DATETIME) 
; 

INSERT INTO Prices 
    (`id`, `price`, `product_id`, `created_at`) 
VALUES 
    (1, 10, 1, '2017-02-07 07:00:00'), 
    (2, 10, 2, '2017-02-07 07:00:00'), 
    (3, 15, 3, '2017-02-07 07:00:00'), 
    (5, 15, 1, '2017-03-07 07:00:00'), 
    (6, 20, 2, '2017-03-07 07:00:00'), 
    (7, 25, 3, '2017-03-07 07:00:00') 
; 

查詢

SELECT 
    Products.id 
, Products.name 
, (current_month.price - last_month.price) AS difference 
, (
    CASE 
     WHEN last_month.price > current_month.price 
     THEN 'lower' 

     WHEN last_month.price < current_month.price 
     THEN 'higher' 

    END 
    ) AS incremental 
, last_month.price 'lastMonth' 
, current_month.price 'now' 
FROM (
SELECT 
    * 
FROM 
    Prices 
WHERE 
    MONTH(created_at) = MONTH((CURDATE() - INTERVAL 1 MONTH)) 
) 
AS 
    last_month 
INNER JOIN (
    SELECT 
    * 
    FROM 
    Prices 
    WHERE 
    MONTH(created_at) = MONTH((CURDATE())) 
) 
AS 
    current_month 
ON 
    last_month.product_id = current_month.product_id 
INNER JOIN 
Products 
ON 
    last_month.product_id = Products.id 
WHERE 
    last_month.price < current_month.price #incremental should be higher 
ORDER BY 
difference DESC 
LIMIT 5 

結果

id name  difference incremental lastMonth  now 
------ ------- ---------- ----------- --------- -------- 
    2 pears   10 higher    10  20 
    3 bananas   10 higher    15  25 
    1 apples   5 higher    10  15 
+1

謝謝,這個作品,雖然我得到了重複的產品。在開始放置「SELECT DISTINCT」似乎解決了這個問題。再次感謝 :) –

2

您可以使用基於fliterd的按月選擇的正確聯接。

這將返回所需的值(你可以添加你需要的文本字符串)

select p.name, m1.price as this_month, m2.price as prev_month, m2.price-m1.price as diff 
from product 
left join (
     select price, product_id 
     from Prices 
     where month(created_at) = month(NOW()) 
     and year(created_at) = year(NOW()) 
    ) m1 on m1.product_id = p.id 
left join (
     select price, product_id 
     from Prices 
     where month(created_at) = MONT(DATE_SUB(NOW(), INTERVAL 1 MONTH) 
     and year(created_at) = year(DATE_SUB(NOW(), INTERVAL 1 MONTH) 
    ) m2 on m2.product_id = p.id 
order by diff desc 
limit 5 
+0

'select top 5'? **我怎樣才能得到5個產品....?** –

+0

@GeomanYabes答案更新 – scaisEdge