【发布时间】:2014-05-31 07:14:00
【问题描述】:
我有两张桌子ms 和fr。
ms 包含列pid,month 和totalsales,而fr 包含pid, month 和forecast。
在两个表中,每个 pid 都有 12 条记录,基于月份。
我实际上想计算公式为(0.3 * totalsales) + (0.7 * previous month result of forecast) 的需求。上个月的预测结果就像用户选择pid2 和month2 一样,会以第1个月的预测结果作为计算数据。
我已经尝试过了,但没有返回想要的结果:
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
当我执行上面的代码时,结果是基于它们的每个forecast 结果。例如,当我选择pid 1、month 2 时,它也会从month 2 中获取forecast 结果。同时我希望它把来自month 1 的forecast 结果作为它的计算数据。
【问题讨论】:
-
这不是很清楚。也许一些样本数据和样本结果(实际的和预期的)将有助于澄清。
-
还要指定第1个月发生了什么,必须从前一年获取数据?
-
例如,pid1,month1 = 100 的总销售额,然后对于 pid1,第 1 个月,forecastdemand = 100,pid1,month2,forecastdemand=0 那么如果我想计算 pid1 的预测, month2 然后计算变为 ((0.3*100)+(0.7*100))。但是当我执行上面的代码时,计算变成了 ((0.3*100) +(0.7*0))
-
Bad habits to kick : using old-style JOINs - 旧式 逗号分隔的表格列表 样式已随 ANSI-92 SQL 标准(超过 20 年前)
标签: c# sql sql-server visual-studio-2010