我有这个问题:
SELECT
mc.equipment as equipment
, CONVERT (DATE, mc.CreatedOn) as Data
, sum(mc.quantity) as Production
, tc.PlannedProduction as ProductionPlanned
, ABS((sum(mc.quantity) - tc.PlannedProduction)) as ProductionVSPlanned
FROM
AT_MACHINE_COUNTER mc INNER JOIN
TC_PlannedProduction tc ON mc.equipment = tc.Workcenter
WHERE
mc.CounterType = 1
and mc.CreatedOn between @DATA_START and @DATA_END
and tc.PlannedDate between @DATA_START and @DATA_END
and mc.equipment = @MACHINE
and mc.CreatedOn IS NOT NULL
GROUP BY CONVERT (DATE, mc.CreatedOn), mc.equipment, tc.PlannedProduction
它有效,这就是结果:
--------------------------------------------------------------------------------------------
| Equipment | Data |Production | PlannedProduction | Production vs Planned|
--------------------------------------------------------------------------------------------
| COMBINER001 |2014-11-24 |1097.8920000000| 16000 | 14902,108 |
| COMBINER001 |2014-11-25 |2065.3200000000| 16000 | 13934,68 |
| COMBINER001 |2014-11-26 |752.1720000000 | 16000 | 15247,828 |
| COMBINER001 |2014-11-27 |1176.9720000000| 16000 | 14823,028 |
---------------------------------------------------------------------------------------------
但是这个结果不正确,因为在TC_PlannedProduction中没有25,26,27-11-2014的值 对于列数据为(25,26,27 / 11/2014)的记录,我期望列PlannedProduction的空值
我们可以帮助我吗? reguards
答案 0 :(得分:1)
您当前使用的JOIN条件将与Equipment和Workcenter匹配任何内容;没有考虑到哪个日期在哪里。
如果您正在尝试将特定日期的计划生产与特定日期的实际生产相匹配,则可以尝试将JOIN
条件更改为LEFT OUTER JOIN
并从TC_PlannedProduction添加一个日期字段(我假设是PlannedDate,但由于我不知道您的数据,我无法确认)。
SELECT
mc.equipment as equipment
, CONVERT (DATE, mc.CreatedOn) as Data
, sum(mc.quantity) as Production
, tc.PlannedProduction as ProductionPlanned
, ABS((sum(mc.quantity) - tc.PlannedProduction)) as ProductionVSPlanned
FROM
AT_MACHINE_COUNTER mc LEFT JOIN
TC_PlannedProduction tc ON
mc.equipment = tc.Workcenter AND
CONVERT (DATE, mc.CreatedOn) = CONVERT (DATE, TC.PlannedDate) -- don't know if you need the second CONVERT() function
WHERE
mc.CounterType = 1
and mc.CreatedOn between @DATA_START and @DATA_END
and tc.PlannedDate between @DATA_START and @DATA_END
and mc.equipment = @MACHINE
and mc.CreatedOn IS NOT NULL
GROUP BY CONVERT (DATE, mc.CreatedOn), mc.equipment, tc.PlannedProduction