程式要做的是把符合條件 MV008 後的 ACTMV.MV016值加總
然後再算出 ACTMV.MV009-ZITMV.MVSUM的結果
※ 有想過用子查詢寫,但一直失敗,只好改暫存處理
※ B段程式在PHP中正常呈現資料,所以不知是A段那邊影響到結果
※ A+B程式在SERVER SQL 中跑結果是正確的
※ KEY的對應 MN、MV 欄位碼數字沒有一致是正確的
/*A段*/
DROP TABLE ZITMV
SELECT MV001,MV002,MV003,MV004,MV005,MV006,SUM(MV016) AS MVSUM
INTO ZITMV
FROM ACTMV
WHERE MV008 <= '20220131'
GROUP BY MV001,MV002,MV003,MV004,MV005,MV006
/*B段*/
SELECT MN001, MN003, MN015, MN006, MN010, MN012, MN013, MN014,MN009-ISNULL(MVSUM,0)AS MN009B
FROM ACTMN LEFT JOIN
ZITMV ON MN001 = MV001 AND MN002 = MV002 AND MN003 = MV003 AND
MN005 = MV004 AND MN006 = MV005 AND MN007 = MV006
把A語句當成Table,與B語句JOIN起來試試。
是指下方B段的部份嗎?還是別的意思?
ACTMN LEFT JOIN
ZITMV ON MN001 = MV001 AND MN002 = MV002 AND MN003 = MV003 AND MN005 = >MV004 AND MN006 = MV005 AND MN007 = MV006
ACTMN LEFT JOIN(
SELECT MV001,MV002,MV003,MV004,MV005,MV006,SUM(MV016) AS MVSUM
-- INTO ZITMV 這就不用了
FROM ACTMV
WHERE MV008 <= '20220131'
GROUP BY MV001,MV002,MV003,MV004,MV005,MV006
) x
想像一下, x 就是一張table表