Sql 从具有“开始日期”列的表中创建日期为行的表
很抱歉标题太复杂,但我的问题是: 我有一个表格,里面有工厂的生产数据。每个工厂有两个产品和关于每个产品生产损失的专栏。工厂也有状态,1表示一切正常,工厂处于全面生产(总生产),2限制生产,3工厂处于维护状态Sql 从具有“开始日期”列的表中创建日期为行的表,sql,sql-server,tsql,sql-server-2012,database-administration,Sql,Sql Server,Tsql,Sql Server 2012,Database Administration,很抱歉标题太复杂,但我的问题是: 我有一个表格,里面有工厂的生产数据。每个工厂有两个产品和关于每个产品生产损失的专栏。工厂也有状态,1表示一切正常,工厂处于全面生产(总生产),2限制生产,3工厂处于维护状态 ID | FactoryID | Status | StartTimestamp | ProductionLoss1 | ProductionLoss2 | GrossProduction1 | GrossProction2 1 | 100 | 1 |2
ID | FactoryID | Status | StartTimestamp | ProductionLoss1 | ProductionLoss2 | GrossProduction1 | GrossProction2
1 | 100 | 1 |2015-01-05 12:15:00.000| 0 | 0 | 100 | 150
2 | 200 | 1 |2015-01-03 12:15:00.000| 0 | 0 | 40 | 90
3 | 100 | 2 |2015-01-29 10:00:00.000| 80 | 100 | 100 | 150
4 | 100 | 1 |2015-02-03 08:00:00.000| 0 | 0 | 100 | 150
5 | 200 | 2 |2015-02-05 08:00:00.000| 100 | 150 | 40 | 90
6 | 200 | 3 |2015-02-06 00:00:00.000| 100 | 150 | 40 | 90
.
.
.
我必须将此表更改为另一个表,其中每个日期都有自己的行和相应的产品以及所有产品的损失产品
产量计算如下:
日产量=24*GrossProction-LostProduction-LostProductionDue维护
LostProduction=如果状态==2,则为24*ProductionLoss
LostProductionDueMaintenance=如果状态==3,则为24*ProductionLoss
输出表应如下所示:
所有工厂应为每个日期和相应的生产数量设置行
DateKey |FactoryID| Production1 | ProductionLoss2 | LostProduction1 | LostProduction2 | LostProductionDueMaintenance1 | LostProductionDueMaintenance2
2015-01-05 | 100 | 2400 | 3600 | 0 | 0 | 0 | 0
2015-01-05 | 200 | 960 | 2160 | 0 | 0 | 0 | 0
.
.
.
2015-01-29 | 100 | 1280 | 2200 | 1120 | 1400 | 0 | 0
.
.
.
2015-02-06 | 200 | 0 | 0 | 0 | 0 | 960 | 2160
我需要生成SQL代码以从Microsoft SQL Server 2012中的上表生成此表。我有一个日期维度来帮助计算。
我认为需要循环日期、所有生产单位和所有可能的状态,但我完全不知道该怎么做
如果有人能给我一点帮助,如何继续将不胜感激 看看这句话。要从2015-01-05 12:15:00.000到达2015-01-05,您可以使用该报表。可能不需要循环