KS, 要做到这一点上的苍蝇,你可以使用子查询。
SELECT product, this_month.units_sold,
(this_month.sales-last_month.sales)*100/last_month.sales,
(this_month.sales-last_year.sales)*100/last_year.sales
FROM (SELECT product, SUM(units_sold) AS units_sold, SUM(sales) AS sales
FROM product WHERE month = 146 GROUP BY product) AS this_month,
(SELECT product, SUM(units_sold) AS units_sold, SUM(sales) AS sales
FROM product WHERE month = 145 GROUP BY product) AS last_month,
(SELECT product, SUM(units_sold) AS units_sold, SUM(sales) AS sales
FROM product WHERE month = 134 GROUP BY product) AS this_year
WHERE this_month.product = last_month.product
AND this_month.product = last_year.product
如果有一个地方产品在一个月内出售,但不是再过一个月,你就必须做一个左连接,并检查空值,特别是如果last_month.sales或last_year.sales 0的情况下。
当前一个月的销售额为404683.00,当前月份的销售额为436493.00时,此增加百分比为1080.00。我很确定答案应该是7.86。? – Perplexed
您可以将select语句的第一行修改为:SELECT product,this_month.units_sold,this_month.sales,last_month.sales,last_year.sales,并查看这三个销售数字是否正确。我只是对我的一个本地表运行这个,它给出了正确的百分比,所以我想看看查询计算的是什么值。问候,布赖恩 –
Nvm我得到它的工作! – Perplexed