我想报告,但我的查询有问题。 这是我的查询:
SELECT a.Tanggal,a.IdMesin,a.PartID,b.PartName,
SUM(a.QtyPlanning) AS Plan,SUM(IF (a.HasilProduksi <> 'Good', a.QtyProduksi, 0)) AS NG,
SUM(IF (a.HasilProduksi = 'Good', a.QtyProduksi, 0)) AS OK
FROM
trans_lhpdthp a
INNER JOIN ms_partcategory b on b.PartID=a.PartID
WHERE a.Tanggal BETWEEN '2018-01-09' AND '2018-01-12'
GROUP BY a.PartID,a.IdMesin
ORDER BY b.PartName
它给我这样的结果:
|Tanggal |IdMesin |PartID|PartName|Plan |NG |OK |
|------- |------ |------|--------|-----|---|----|
|2018-01-09|BS 120 A|BLXX |BOTTOM |5000 |500|4500|
|2018-01-09|BS 120 B|ACLA |CLAMP |5000 |600|4400|
|2018-01-12|BS 260 E|SMXX |RIGHT |1200 |0 |1200|
我希望得到这样的结果:
|Tanggal |IdMesin |PartID|PartName|Plan |NG |OK |
|------- |------ |------|--------|-----|---|----|
|2018-01-09|BS 120 A|BLXX |BOTTOM |5000 |500|4500|
|2018-01-09|BS 120 B|ACLA |CLAMP |5000 |600|4400|
|2018-01-10| | | |0 |0 |0 |
|2018-01-11| | | |0 |0 |0 |
|2018-01-12|BS 260 E|SMXX |RIGHT |1200 |0 |1200|
你能帮助我,可以感激任何帮助。
答案 0 :(得分:0)
您会注意到INNER JOIN与LEFT JOIN问题导致您的问题。当b中没有数据时INNER JOIN将不会显示来自a的记录。但即使你没有b中的相关记录,LEFT JOIN也会带来记录。
以下情况应该有效。
SELECT
a.Tanggal,
a.IdMesin,
a.PartID,
b.PartName,
SUM(a.QtyPlanning) AS Plan,
SUM(IF (a.HasilProduksi <> 'Good', a.QtyProduksi, 0)) AS NG,
SUM(IF (a.HasilProduksi = 'Good', a.QtyProduksi, 0)) AS OK
FROM
trans_lhpdthp a
LEFT JOIN ms_partcategory b
ON b.PartID = a.PartID
WHERE a.Tanggal BETWEEN '2018-01-09' AND '2018-01-12'
GROUP BY a.PartID,a.IdMesin
ORDER BY b.PartName
答案 1 :(得分:0)
实现目标有两种方法