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