postgres查询销售预测

时间:2010-08-05 17:24:35

标签: sql postgresql

=================

这是我的查询:

SELECT 
SUM(sub_total) AS sales,
CASE    WHEN (sub_total<100) THEN '0-99'    
    WHEN (sub_total>=100 AND sub_total<200) THEN '100-199'  
    WHEN (sub_total>=200 AND sub_total<300) THEN '200-299'  
    WHEN (sub_total>=300 AND sub_total<400) THEN '300-399'  
    WHEN (sub_total>=400 AND sub_total<500) THEN '400-499'  
    ELSE '500+'
END
AS product_sales_range
FROM order_item 
GROUP BY sub_total

========================

它给出的结果为:

sales       product_sales_range
-----       -------------------

10398.96        200-299
600         100-199
300         0-99
7699.78     300-399
6799.32     100-199
600         0-99
26599.24        500+

==========================================

我想得到这样的结果:

sales       product_sales_range sales_increase_by_10%   sales_increase_by_20%
-----       ------------------- ------------------  ------------------

10398.96        200-299
600         100-199
300         0-99
7699.78     300-399
6799.32     100-199
600         0-99
26599.24        500+

============

要求:

目前我们有销售数据。需要找出销售预测,好像销售额将增加10%,20%,30%等。示例:今天product_sales_range(0-99)的销售额为300.如果销售额增加10%,则sales_increase_by_10 %应为330

1 个答案:

答案 0 :(得分:2)

我错过了什么,或者您只是需要使用:

  SELECT SUM(sub_total) AS sales,
         CASE WHEN (sub_total<100) THEN '0-99' 
              WHEN (sub_total>=100 AND sub_total<200) THEN '100-199' 
              WHEN (sub_total>=200 AND sub_total<300) THEN '200-299' 
              WHEN (sub_total>=300 AND sub_total<400) THEN '300-399' 
              WHEN (sub_total>=400 AND sub_total<500) THEN '400-499' 
              ELSE '500+'
         END AS product_sales_range,
         SUM(sub_total) * 1.1 AS sales_increase_by_10Percent,
         SUM(sub_total) * 1.2 AS sales_increase_by_20Percent
    FROM order_item 
GROUP BY sub_total