duanqiechui2378 2014-12-03 20:47
浏览 171
已采纳

计算与平均值的百分比差异

I'd like to get a price level percentage (lower or higher than average product price) by store but I really don't know where to start. How can I calculate this?

I've got a MySQL table with data like this:

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 

I'd like to calculate it per store with all the products but to make this example easier I use only the product with id 1. I think:

10 is the average price of product 1, because it's between 8 and 12. So 10 equals 0% for store 1. To calculate the price level for store 3 I can do this: 100 - (8 * 100 / 10) = 20%. But how can I do this with multiple products? And not every store does have all the products and eventually I'd like to fit it in one MySQL query if possible with a result like:

store_id  |  percentage
1         |  0
2         |  -20
3         |  20
  • 写回答

3条回答 默认 最新

  • dplase3140 2014-12-03 22:18
    关注

    I broke this down into multiple pieces and then put them back together.

    First, I got the average price for each product_id:

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

    The following makes the assumption that a product_id cannot appear at a store more than once. So I've joined the tables together, so I can see what the store charges next to the average price for that item:

    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;
    

    Once I had that, I was able to take the difference between averagePrice and price, divide by average price and multiply by 100 like this:

    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;
    

    It worked for me in SQL Fiddle.

    EDIT

    To get the average price difference per store (for all items) I believe you can just take the above, and by averaging the stores price difference on each individual item, you'll get the average price difference for that store, like this:

    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;
    

    Here it is in Fiddle.

    EDIT 2

    Again, I will rework this in pieces and try and put it back together. I know I will need a subquery to get the number of stores (so I know if a product is sold at each store) and I can use this:

    SELECT COUNT(distinct store_id) AS storecount
    FROM myTable;
    

    Now, I can use that as a subquery to get products sold at every store. I can group by product_id and amount, so that if every store has the item at amount 1, and every store has item at amount 2, it will show up each time.

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

    I can add to the above to get the average price of each item for that amount:

    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);
    

    Once I have that, I can calculate the average price difference for each store using the same method I used earlier, like this:

    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 will return the store, product_id, the amount of the product, and the store's difference from the average price for that product for that amount. If you want the average price difference for the store on all items, try this:

    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;
    

    Again, this will only include items sold at every store, that has the same amount discount at every store.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行