2014-12-03 749 views
0

我想通過商店獲得價格水平百分比(低於或高於平均產品價格),但我真的不知道從哪裏開始。我如何計算這個?計算與平均值的差異百分比

我有一個MySQL表的數據是這樣的:

product_id | store_id | price | amount 
1   | 1  | 10.00 | 1 
1   | 2  | 12.00 | 1 
1   | 3  | 8.00 | 1 
2   | 1  | 21.20 | 1 
2   | 3  | 29.50 | 1 
2   | 3  | 40.00 | 2 

我想和所有的產品來計算的話每家商店,但使這個例子更容易我只用了產品ID爲1 。我認爲:

10是產品1的平均價格,因爲它在8和12之間。所以10等於商店1的0%。要計算商店3的價格水平,我可以這樣做:100 - (8 * 100/10)= 20%。但是我怎麼能用多種產品來做到這一點?而不是每家商店確實有所有的產品,最終我想如果可能的話,以適應它在一個MySQL查詢與像結果:

store_id | percentage 
1   | 0 
2   | -20 
3   | 20 
+1

商店可以有多個產品嗎? – AdamMc331 2014-12-03 22:10:11

+0

@ McAdam331這是極不可能的。我們假設不是! – Strawberry 2014-12-04 01:02:42

+0

@ McAdam331是的,這是可能的。我已經改變了示例表,忘記了這一點。謝謝! – Roy 2014-12-04 08:14:07

回答

1

我把它分解成多個部分,然後放回到一起。

首先,我得到了平均價格爲每臺的product_id:

SELECT product_id, AVG(price) AS averagePrice 
FROM myTable 
GROUP BY product_id; 

下使一個PRODUCT_ID不能在商店出現不止一次的假設。所以我加入了表一起,這樣我就可以看到什麼店費旁邊的平均價格爲該項:

SELECT m.product_id, m.store_id, m.price, t.averagePrice 
FROM myTable m 
JOIN(
    SELECT product_id, AVG(price) AS averagePrice 
    FROM myTable 
    GROUP BY product_id) t ON t.product_id = m.product_id; 

有一次,我說,我能夠採取averagePrice和價格之間的差額,按平均價格除以和100這樣的繁殖:

SELECT m.product_id, m.store_id, (100 * ((m.price - t.averagePrice)/t.averagePrice)) AS difference 
FROM myTable m 
JOIN(
    SELECT product_id, AVG(price) AS averagePrice 
    FROM myTable 
    GROUP BY product_id) t ON t.product_id = m.product_id; 

它在SQL Fiddle爲我工作。

編輯

爲了讓每家店的平均價格差異(對所有項目),我相信你可以走在上面,並通過平均每個單項商店價格差別,你會得到對於存儲的平均價格差異,像這樣:

SELECT store_id, AVG(difference) AS averagePriceDifference 
FROM(
    SELECT m.product_id, m.store_id, (100 * ((m.price - t.averagePrice)/t.averagePrice)) AS difference 
    FROM myTable m 
    JOIN(
    SELECT product_id, AVG(price) AS averagePrice 
    FROM myTable 
    GROUP BY product_id) t ON t.product_id = m.product_id) t 
GROUP BY store_id; 

這是Fiddle

EDIT 2

而且,我將在片返工這和嘗試,並把它重新走到一起。我知道我需要一個子查詢得到的門店數(所以我知道,如果一個產品在每家商店出售),我可以用這個:

SELECT COUNT(distinct store_id) AS storecount 
FROM myTable; 

現在,我可以把它作爲一個子查詢來獲得在每家商店銷售的產品。我可以按product_id和金額進行分組,因此如果每家商店的商品數量爲1,並且每家商店的商品數量爲2,則每次都會顯示該商品。

SELECT product_id, amount 
FROM myTable 
GROUP BY product_id, amount 
HAVING COUNT(distinct store_id) = (SELECT COUNT(distinct store_id) FROM myTable); 

我可以添加到上面來獲得每個項目的平均價格爲金額:

SELECT product_id, amount, AVG(price) AS averagePriceForAmount 
FROM myTable 
GROUP BY product_id, amount 
HAVING COUNT(distinct store_id) = (SELECT COUNT(distinct store_id) FROM myTable); 

一旦我有,我可以使用相同的計算每個店的平均價格差異方法我用了前面,如:

SELECT m.store_id, m.product_id, m.amount, (100 * ((m.price - t.averagePriceForAmount)/t.averagePriceForAmount)) AS differenceForItemAndAmount 
FROM myTable m 
JOIN(
    SELECT product_id, amount, AVG(price) AS averagePriceForAmount 
    FROM myTable 
    GROUP BY product_id, amount) t ON t.product_id = m.product_id AND t.amount = m.amount 
GROUP BY m.store_id; 

This將返回店裏,PRODUCT_ID,該產品的量,和店裏的均價從在t差異這個數量的帽子產品。如果你想對所有項目的商店中的平均差價,試試這個:

SELECT store_id, AVG(differenceForItemAndAmount) AS averageDifferenceForStore 
FROM(
    SELECT m.store_id, m.product_id, m.amount, (100 * ((m.price - t.averagePriceForAmount)/t.averagePriceForAmount)) AS differenceForItemAndAmount 
    FROM myTable m 
    JOIN(
    SELECT product_id, amount, AVG(price) AS averagePriceForAmount 
    FROM myTable 
    GROUP BY product_id, amount) t ON t.product_id = m.product_id AND t.amount = m.amount 
    GROUP BY m.store_id) t 
GROUP BY store_id; 

同樣,這將只包括在每一家商店,有在每家商店的相同數量的折扣銷售的項目。

+0

我只是在我發佈在Multisync答案中的評論中複製,因爲它的確如此:我想爲每個商店獲取所有產品的百分比,而不是每個產品的百分比。但感謝您的幫助! – Roy 2014-12-04 08:30:30

+0

如果今天有空,我會盡量調整它,如果這仍然是你的一個公開問題。 – AdamMc331 2014-12-04 12:49:21

+0

另外,@羅伊不會改變你的預期結果嗎? – AdamMc331 2014-12-04 12:49:50

0
select t1.product_id, t1.store_id, 
     100 - (t1.price * 100/t2.avg_price) 
from tab t1 join (select product_id, avg(price) as avg_price 
        from tab group by product_id) t2 
      on t1.product_id = t2.product_id; 

中計算平均的子查詢每一個產品然後加入該表格可以生成您需要的結果。

+0

我想爲每個商店獲取所有產品的百分比,而不是每個產品的百分比。但感謝您的幫助! – Roy 2014-12-04 08:20:30

1

寫查詢來獲取每個產品的平均價格:

select product_id, avg(price) as average_price 
from mysqltable 
group by product_id 

使用它作爲一個子查詢,並從它指的平均價格(使用你的計算值平均價格的偏差)

select product_id, store_id, 100 - (price * 100/average_price) 
from mysqltable a 
inner join  (select product_id, avg(price) as average_price 
       from mysqltable 
       group by product_id) b 
on a.product_id = b.product_id 
+0

我只是在我發佈在Multisync答案中的評論中複製,因爲它的確如此:我想爲每個商店獲取所有產品的百分比,而不是每個產品的百分比。但感謝您的幫助! – Roy 2014-12-04 08:26:20

+0

@roy我們可以指出......每種產品都是爲加權平均而加權的? – Twelfth 2014-12-04 15:52:04

+0

看到我對McAdam331的評論他的回答。 – Roy 2014-12-04 16:01:08