我的背景是Excel建模,我对SQL很新。我有一个如下所示的表格。我想创建'desired_col',假设预测月份的月复合增长率为1%:
region | category | date | date_type | revenue | desired_col
---------------------------------------------------------------------
East | Inventory | 07/2017 | Actual | 25 | 25
East | Non-Inventory | 07/2017 | Actual | 20 | 20
West | Inventory | 07/2017 | Actual | 18 | 18
West | Non-Inventory | 07/2017 | Actual | 16 | 16
East | Inventory | 08/2017 | Forecast | 0 | 25.25
East | Non-Inventory | 08/2017 | Forecast | 0 | 20.2
West | Inventory | 08/2017 | Forecast | 0 | 18.18
West | Non-Inventory | 08/2017 | Forecast | 0 | 16.16
East | Inventory | 09/2017 | Forecast | 0 | 25.5025
East | Non-Inventory | 09/2017 | Forecast | 0 | 20.402
West | Inventory | 09/2017 | Forecast | 0 | 18.3618
West | Non-Inventory | 09/2017 | Forecast | 0 | 16.3216
现在,我可以使用LAG函数在预测的第一个月(上面的例子中的八月)中完成此操作:
CASE WHEN date_type = 'Actual' THEN revenue ELSE
LAG( revenue , 1 ) OVER ( PARTITION BY region, category ORDER BY date ) * 1.01
END
但上述声明在9月及以后返回0。这在Excel中很简单,但我很难过。你能给我什么建议吗?谢谢!
答案 0 :(得分:1)
您可以根据上一个实际月份和预测月份之间的月数来确定必要的偏移量,并将其用作LAG
的动态偏移量和增长率的功率:
with
get_offsets as (
select *
,case when to_date(date,'MM/YYYY')>'2017-07-01' then datediff(month,'2017-07-01',to_date(date,'MM/YYYY'))::integer end as this_offset
from your_table
)
select *
,case when date_type = 'Actual' then revenue
else lag(revenue, this_offset) over (partition by region, category order by date) * 1.01 ^ this_offset
end
from get_offsets
答案 1 :(得分:0)
LAG声明不适用于9月以来,因为滞后(1)将关注8月9月的收入零,将使用8月的零计算,10月将使用零计算九月等等。
您需要像这样循环LAG语句:
LAG( LAG( revenue , 1 ) OVER ( PARTITION BY region, category ORDER BY date ) * 1.01 , 1 ) OVER ( PARTITION BY region, category ORDER BY date ) * 1.01
...但是你需要这么做几个月才能预测到未来。 I.E.很乱。
我认为你将不得不使用循环和变量(就像那样是非SQL)。但好消息是,它更符合您熟悉的Excel解决方案。