【发布时间】:2012-07-05 22:17:32
【问题描述】:
简单地说, 我有这些表。
Planning:
Date | Machine | Product | PlanningParts
OKParts:
Date | Machine | Product | OKParts
Scrap:
Date | Machine | Product | ScrapParts
Trials:
Date | Machine | Product | TrialParts
Breakdowns:
Date | Machine | Product | Minutes
这些表可以有两个或多个相同 [Date |机器 | Product],它们不是唯一的列。 获得此输出的最佳 SQL 代码(SQLite 或 SQL Server)是什么?
Output:
Date | Machine | Product | Planning | OKParts | ScrapParts | TrialParts | Minutes
编辑: 在输出中,每个 [Date,Machine,Product] 我需要一行,其余列必须是 SUM (我不会说英语,对不起)
编辑: 例子: (我不会把表格“试验”缩短)
Planning:
Date | Machine | Product | PlanningParts
1/6/12 | Blower | A001 | 100
2/6/12 | Blower | A002 | 100
2/6/12 | Assembly| B001 | 50
OKParts:
Date | Machine | Product | OKParts
1/6/12 | Blower | A001 | 50
1/6/12 | Blower | A001 | 20
1/6/12 | Blower | A002 | 100
Scrap:
Date | Machine | Product | ScrapParts | Reason
1/6/12 | Blower | A001 | 5 | Low Weight
1/6/12 | Blower | A001 | 3 | High Weight
2/6/12 | Assembly| B001 | 4 | Bad Cut
Breakdowns
Date | Machine | Product | Minutes | Reason
1/6/12 | Blower | A001 | 100 | Manteinance
1/6/12 | Blower | A001 | 20 | Manteinance
2/6/12 | Assembly| B001 | 100 | Quality approval
OUTPUT:
Date | Machine | Product | Planning | OKParts | ScrapParts | Breakdowns
1/6/12 | Blower | A001 | 100 | 70 | 8 | 120
1/6/12 | Blower | A002 | 100 | 100 | 0 | 0
2/6/12 | Assembly| B001 | 50 | 0 | 4 | 100
【问题讨论】:
-
请显示示例数据(包括几行,其中有多个值对单个 SUM 有贡献)和所需的结果。这样,我们就不必就结果提出 15 个后续问题,或者猜测您需要的查询。如果您提供样本数据和期望的结果,我们可以在向您展示解决方案并让您因为您的要求不明确而拒绝它们之前对其进行测试和验证。
-
如果一张表中有记录,那么同一个{Date,Machine,Product}总是会有至少条记录吗?其他表?或者他们有时可能有 0 条该 {Date,Machine,Product} 的记录?和/或是否有一张表总是有至少一条记录用于该{Date,Machine,Product}?
-
@Aaron Bertrand - 对不起,我还没有阅读第一条评论
-
@Dems - 他们有时有该 {Date,Machine,Product} 的 0 条记录
标签: sql sql-server sqlite