我在一家小公司工作,我们正试图摆脱Excel工作簿的库存控制。我以为我在(纳赛尔)的帮助下想到了它,但它超越了我。这就是我可以进入一个表格,从那里我需要它看起来像下表。
我的数据
ID|GrpID|InOut| LoadFt | LoadCostft| LoadCost | RunFt | RunCost| AvgRunCostFt
1 1 1 4549.00 0.99 4503.51 4549.00 0 0
2 1 1 1523.22 1.29 1964.9538 6072.22 0 0
3 1 2 -2491.73 0 0 3580.49 0 0
4 1 2 -96.00 0 0 3484.49 0 0
5 1 1 8471.68 1.41 11945.0688 11956.17 0 0
6 1 2 -369.00 0 0 11468.0568 0 0
7 2 1 1030.89 5.07 5223.56 1030.89 0 0
8 2 1 314.17 5.75 1806.4775 1345.06 0 0
9 2 1 239.56 6.3 1508.24 1509.228 0 0
10 2 2 -554.46 0 0 954.768 0 0
11 2 1 826.24 5.884 4861.5961 1781.008 0 0
预期输出
ID|GrpID|InOut| LoadFt | LoadCostft| LoadCost | RunFt | RunCost| AvgRunCostFt
1 1 1 4549.00 0.99 4503.51 4549.00 4503.51 0.99
2 1 1 1523.22 1.29 1964.9538 6072.22 6468.4638 1.0653
3 1 2 -2491.73 1.0653 -2490.6647 3580.49 3977.7991 1.111
4 1 2 -96.00 1.111 -106.656 3484.49 3871.1431 1.111
5 1 1 8471.68 1.41 11945.0688 11956.17 15816.2119 1.3228
6 1 2 -369.00 1.3228 -488.1132 11468.0568 15328.0987 1.3366
7 2 1 1030.89 5.07 5223.56 1030.89 5223.56 5.067
8 2 1 314.17 5.75 1806.4775 1345.06 7030.0375 5.2266
9 2 1 239.56 6.3 1508.24 1509.228 8539.2655 5.658
10 2 2 -554.46 5.658 -3137.1346 954.768 5402.1309 5.658
11 2 1 826.24 5.884 4861.5961 1781.008 10263.727 5.7629
小组的第一个记录将被视为期初余额。进入院子的库存ID为1,院子外的库存为2。进入院子的负载镜头总是有每英尺的负载成本,我可以计算镜头的运行总数。一组的第一个记录很容易计算出每英尺的运行成本和运行成本。下一条记录变得有点难以计算。我需要将每英尺的运行成本的平均值向前移动到每英尺的负荷成本,然后再计算出每英尺的运行成本和平均运行成本。希望这对某人有意义,我们可以自动完成其中一些计算。谢谢你的帮助。
这是我发现的一个Oracle示例;
SQL> select order_id
2 , volume
3 , price
4 , total_vol
5 , total_costs
6 , unit_costs
7 from ( select order_id
8 , volume
9 , price
10 , volume total_vol
11 , 0.0 total_costs
12 , 0.0 unit_costs
13 , row_number() over (order by order_id) rn
14 from costs
15 order by order_id
16 )
17 model
18 dimension by (order_id)
19 measures (volume, price, total_vol, total_costs, unit_costs)
20 rules iterate (4)
21 ( total_vol[any] = volume[cv()] + nvl(total_vol[cv()-1],0.0)
22 , total_costs[any]
23 = case SIGN(volume[cv()])
24 when -1 then total_vol[cv()] * nvl(unit_costs[cv()-1],0.0)
25 else volume[cv()] * price[cv()] + nvl(total_costs[cv()-1],0.0)
26 end
27 , unit_costs[any] = total_costs[cv()] / total_vol[cv()]
28 )
29 order by order_id
30 /
ORDER_ID VOLUME PRICE TOTAL_VOL TOTAL_COSTS UNIT_COSTS
---------- ---------- ---------- ---------- ----------- ----------
1 1000 100 1000 100000 100
2 -500 110 500 50000 100
3 1500 80 2000 170000 85
4 -100 150 1900 161500 85
5 -600 110 1300 110500 85
6 700 105 2000 184000 92
选择了6行。
答案 0 :(得分:0)
首先让我说三件事:
现在进入解决方案:
我尝试在一个结构良好的单class Project
{
public:
void do_stuff() {}
};
// This class is the Project Owner
// Meaning it is responsible for
// managing its life-time
class App
{
public:
// return a reference expressing the caller
// does not gain ownership merely access
Project& GetProject() { return *project; }
// replace configured Project
// accepting a std::unique_ptr expresses the fact that
// this function takes ownership of the Project
void SetProject(std::unique_ptr<Project> project) { this->project = std::move(project); }
private:
std::unique_ptr<Project> project;
};
int main()
{
App app; // don't make pointers if we don't have to
// project has ownership of the raw pointer
std::unique_ptr<Project> project(new Project());
// std::move() transfers ownership to the function's parameter
app.SetProject(std::move(project));
// get a reference to the internal Project
Project& retrievedProject = app.GetProject();
// work with configured Project
retrievedProject.do_stuff();
}
语句中使用LAG
语句的初始提示,但因为你只能在update
内使用窗口函数(也就是LAG)或select
条款,这是行不通的。
下面的代码简称:
它可以计算每个记录的各种计算字段,并且可以使用适当的函数计算每个记录,更新表格然后移动到下一条记录。
有关其他信息,请参阅代码中的注释
order by
是一个演示表(在链接的SQLFiddle中可见)
有关TempTable
decimal(19, 4)
带有示例数据的输出和SQLFiddle演示如何运作:
ID GrpID InOut LoadFt RunFt LoadCost RunCost LoadCostFt AvgRunCostFt 1 1 1 4549 4549 4503.51 4503.51 0.99 0.99 2 1 1 1523.22 6072.22 1964.9538 6468.4638 1.29 1.0653 3 1 2 -2491.73 3580.49 -2654.44 3814.0238 1.0653 1.0652 4 1 2 -96 3484.49 -102.2592 3711.7646 1.0652 1.0652 5 1 1 8471.68 11956.17 11945.0688 15656.8334 1.41 1.3095 6 1 2 -369 11587.17 -483.2055 15173.6279 1.3095 1.3095 7 2 1 1030.89 1030.89 5226.6123 5226.6123 5.07 5.07 8 2 1 314.17 1345.06 1806.4775 7033.0898 5.75 5.2288 9 2 1 239.56 1584.62 1509.228 8542.3178 6.3 5.3908 10 2 2 -554.46 1030.16 -2988.983 5553.3348 5.3908 5.3907 11 2 1 826.24 1856.4 4861.5962 10414.931 5.884 5.6103
如果您不清楚部分代码,我可以通过其他解释进行更新。