如何采取上个月的结果sql [英] how to take previous month result in sql
问题描述
我有两个表'ms'和'fr'。
'ms'包含pid,month和totalsales,同时'fr'包含pid,month,n预测。在两个表中,每个pid有12个基于月的记录。
我实际上想要计算公式=(0.3 * totalsales)+(0.7 *上个月的预测结果)的需求。上个月的预测结果就像用户选择pid2和第2个月一样,然后将第1个月的预测结果作为计算数据。
我已经尝试了但是想要的结果还没有出来。
以下是我的代码
i have 2 tables which is 'ms' and 'fr'.
'ms' contain the pid,month, and totalsales meanwhile 'fr' contain pid, month, n forecast. IN both table, each pid has 12 record based on month.
i actually want to calculate the demand where the formula is = (0.3* totalsales)+(0.7* previous month result of forecast). The previous month result of forecast is like when the user choose pid2 and month 2 , then it will take the forecast result from month 1 as its calculation data.
i tried it already but the desired result is not coming out.
below is my code
select ((0.3*totalsalesamount)+(0.7*forecastdemand)) as demand from Monthlysales, forecastreorder where Monthlysales.P_ID = forecastreorder.Productid and Monthlysales.P_ID = 1 and forecastreorder.Month = 2
当我执行上面的代码时,结果基于他们的每个预测结果。例如,当我选择pid 1,第2个月时,它也将从第2个月获取预测结果。同时我希望它将第1个月的预测结果作为计算数据。
我该怎么做?
请帮帮我,谢谢高级:)
when i execute the code above, the result is based on their each forecast result. for example, when i choose pid 1, month 2, then it will take the forecast result from month 2 also. meanwhile i want it to take the forecast result from month 1 as its calculation data.
how can i do that??
please help me, thanks in advanced :)
推荐答案
试试这:
Try this:
select ((0.3*totalsalesamount)+(0.7*forecastdemand)) as demand
from Monthlysales join forecastreorder
on Monthlysales.P_ID = forecastreorder.Productid
where Monthlysales.P_ID = 1
and Monthlysales.month = 2
and forecastreorder.Month = 1
这篇关于如何采取上个月的结果sql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!