现在我有 3 张桌子
订单:
身份证 | 日期 | 机器 | 规划
好片:
身份证 | 日期 | 机器 | 件 | 产品
坏片:
身份证 | 日期 | 机器 | 件 | 产品 | 零件
查询的输出是
日期 | 机器 | 规划 | 好片| 坏件
实际的查询是这样的:
SELECT
data.Date AS Date,
data.Machine AS Machine,
SUM(CASE WHEN data.type = 'Planning' THEN data.value END ) AS Planning,
SUM(CASE WHEN data.type = 'GoodPieces' THEN data.value END ) AS GoodPieces,
SUM(CASE WHEN data.type = 'BadPieces' THEN data.value END ) AS BadPieces
FROM
( SELECT
'Planning' AS Type,
Date AS Date,
Machine AS Machine,
Planning AS Value
FROM Orders
UNION ALL
SELECT
'GoodPieces',
Date,
Machine,
Pieces AS Value
FROM GoodPieces
UNION ALL
SELECT
'BadPieces'
Date,
Machine,
Pieces AS Value
FROM BadPieces ) AS data
GROUP BY
Date,
Machine
我的问题是是否有办法用这 3 个表获得相同的输出
订单:
身份证 | 日期 | 机器| 规划
好片:
订单号 | 件 | 产品
坏片:
订单号 | 件 | 产品 | 零件
谢谢,