SELECT dbo.SASALEHD.Ref_Doc2 AS [JobID] ,dbo.SASALEHD.Doc_No, dbo.SMITEMMS.Code,dbo.SASALEHD.Doc_Date
FROM dbo.SMITEMMS INNER JOIN dbo.SASALEDT ON dbo.SMITEMMS.ID = dbo.SASALEDT.SMITEMMS_ID RIGHT OUTER JOIN
dbo.SASALEHD ON dbo.SASALEDT.SASALEHD_ID = dbo.SASALEHD.ID
GROUP BY dbo.SASALEHD.Doc_No, dbo.SASALEHD.Ref_Doc2, dbo.SMITEMMS.Code,dbo.SASALEHD.Doc_Date
HAVING(dbo.SASALEHD.Doc_No LIKE 'SS%')";
Query 2
SELECT dbo.PAPUROHD.Remark3 AS [JobID],dbo.PAPUROHD.Doc_No AS [PRQ NO], dbo.PAPUROHD.Doc_Date AS [PRQ Date], dbo.PAPURODT.Link_DocNo AS [GR No], dbo.IMTRNSHD.Doc_Date AS [GR Date]
FROM dbo.IMTRNSHD INNER JOIN dbo.PAPURODT ON dbo.IMTRNSHD.Doc_No = dbo.PAPURODT.Link_DocNo RIGHT OUTER JOIN
dbo.PAPUROHD ON dbo.PAPURODT.PAPUROHD_ID = dbo.PAPUROHD.ID
GROUP BY dbo.PAPUROHD.Doc_No, dbo.PAPURODT.Link_DocNo, dbo.PAPUROHD.Doc_Date, dbo.IMTRNSHD.Doc_Date, dbo.PAPUROHD.Remark3
HAVING (dbo.PAPUROHD.Doc_No LIKE 'PQ%')";_No LIKE 'PQ%')";
SELECT tb1.* FROM ( SELECT dbo.SASALEHD.Ref_Doc2 AS [JobID] ,dbo.SASALEHD.Doc_No, dbo.SMITEMMS.Code,dbo.SASALEHD.Doc_Date
FROM dbo.SMITEMMS INNER JOIN dbo.SASALEDT ON dbo.SMITEMMS.ID = dbo.SASALEDT.SMITEMMS_ID RIGHT OUTER JOIN
dbo.SASALEHD ON dbo.SASALEDT.SASALEHD_ID = dbo.SASALEHD.ID
GROUP BY dbo.SASALEHD.Doc_No, dbo.SASALEHD.Ref_Doc2, dbo.SMITEMMS.Code,dbo.SASALEHD.Doc_Date
HAVING(dbo.SASALEHD.Doc_No LIKE 'SS%') ) tb1
LEFT JOIN
(SELECT dbo.PAPUROHD.Remark3 AS [JobID],dbo.PAPUROHD.Doc_No AS [PRQ NO], dbo.PAPUROHD.Doc_Date AS [PRQ Date], dbo.PAPURODT.Link_DocNo AS [GR No], dbo.IMTRNSHD.Doc_Date AS [GR Date]
FROM dbo.IMTRNSHD INNER JOIN dbo.PAPURODT ON dbo.IMTRNSHD.Doc_No = dbo.PAPURODT.Link_DocNo RIGHT OUTER JOIN
dbo.PAPUROHD ON dbo.PAPURODT.PAPUROHD_ID = dbo.PAPUROHD.ID
GROUP BY dbo.PAPUROHD.Doc_No, dbo.PAPURODT.Link_DocNo, dbo.PAPUROHD.Doc_Date, dbo.IMTRNSHD.Doc_Date, dbo.PAPUROHD.Remark3
HAVING (dbo.PAPUROHD.Doc_No LIKE 'PQ%')";_No LIKE 'PQ%')) tb2
ON tb1.xxx = tb2.xxx
ได้ทำแบบที่แนะนำแล้วค่ะ ซึ่งในSQL Server แสดงข้อมูลได้ถูกต้อง
แต่เอาSQLมาเขียนใน PHP ไม่มีข้อมูลแสดงเลยค่ะ ไม่ทราบว่าทำผิดตรงไหนหรือเปล่าคะ
$sql = "SELECT tb1.Job_ID, tb1.Job_No, tb1.BOM, tb1.Prod_No, tb1.Op_No, tb1.Op_Name, tb1.Issue_No, tb1.Issue_Date, tb1.AM_No, tb1.AM_Date, tb1.Good_Qty,tb2.AM_Date AS รับผลผลิต
FROM (SELECT dbo.SASALEHD.Ref_Doc2 AS Job_ID, dbo.SFJOBOHD.Job_No, dbo.SMBOMMHD.Code AS BOM, dbo.SFPRODHD.Doc_No AS Prod_No,
dbo.SFPRODDT.Op_No, dbo.SFPRODDT.Op_Name, MAX(dbo.SFISSUHD.Doc_No) AS Issue_No, MAX(dbo.SFISSUHD.Doc_Date) AS Issue_Date,
MAX(dbo.SFPRODAC.Doc_No) AS AM_No, MAX(dbo.SFPRODAC.Actual_Date) AS AM_Date, MAX(dbo.SFPRODAC.Good_Qty) AS Good_Qty
FROM dbo.SFPRODAC LEFT OUTER JOIN
dbo.SFPRODRL ON dbo.SFPRODAC.ID = dbo.SFPRODRL.SFPRODAC_ID RIGHT OUTER JOIN
dbo.SFPRODDT ON dbo.SFPRODAC.SFPRODDT_ID = dbo.SFPRODDT.ID LEFT OUTER JOIN
dbo.SFISSUHD LEFT OUTER JOIN
dbo.SFISSULT ON dbo.SFISSUHD.id = dbo.SFISSULT.SFISSUHD_ID RIGHT OUTER JOIN
dbo.SFISSUDT ON dbo.SFISSUHD.id = dbo.SFISSUDT.SFISSUHD_ID ON dbo.SFPRODDT.ID = dbo.SFISSUDT.In_SFPRODDT_ID RIGHT OUTER JOIN
dbo.SFPRODHD ON dbo.SFPRODDT.SFPRODHD_ID = dbo.SFPRODHD.ID RIGHT OUTER JOIN
dbo.SFJOBODT INNER JOIN
dbo.SFJOBOHD ON dbo.SFJOBODT.SFJOBOHD_ID = dbo.SFJOBOHD.ID INNER JOIN
dbo.SASALECode (PHP)
DT ON dbo.SFJOBODT.SASALEDT_ID = dbo.SASALEDT.ID INNER JOIN
dbo.SASALEHD ON dbo.SASALEDT.SASALEHD_ID = dbo.SASALEHD.ID INNER JOIN
dbo.SMBOMMHD ON dbo.SASALEDT.SMBOMMHD_ID = dbo.SMBOMMHD.ID ON dbo.SFPRODHD.SFJOBOHD_ID = dbo.SFJOBOHD.ID
GROUP BY dbo.SFJOBOHD.Job_No, dbo.SMBOMMHD.Code, dbo.SFPRODHD.Doc_No, dbo.SFPRODDT.Op_No, dbo.SFPRODDT.Op_Name,dbo.SASALEHD.Ref_Doc2
HAVING (NOT (dbo.SFJOBOHD.Job_No LIKE '%AF%')) AND (NOT (dbo.SFJOBOHD.Job_No LIKE '%SL%'))
ORDER BY Job_ID, dbo.SFJOBOHD.Job_No, dbo.SFPRODDT.Op_No) AS tb1
LEFT OUTER JOIN
(SELECT SASALEHD_1.Ref_Doc2 AS Job_ID, MAX(SFJOBOHD_1.Job_No) AS Job_No, SMBOMMHD_1.Code AS BOM,
MAX(SFPRODHD_1.Doc_No) AS Prod_No, MAX(SFPRODDT_1.Op_No) AS Op_No, MAX(SFPRODDT_1.Op_Name) AS Op_Name, MAX(SFISSUHD_1.Doc_No)
AS Issue_No, MAX(SFISSUHD_1.Doc_Date) AS Issue_Date, MAX(SFPRODAC_1.Doc_No) AS AM_No, MAX(SFPRODAC_1.Actual_Date) AS AM_Date,
MAX(SFPRODAC_1.Good_Qty) AS Good_Qty
FROM dbo.SFPRODAC AS SFPRODAC_1 LEFT OUTER JOIN
dbo.SFPRODRL AS SFPRODRL_1 ON SFPRODAC_1.ID = SFPRODRL_1.SFPRODAC_ID RIGHT OUTER JOIN
dbo.SFPRODDT AS SFPRODDT_1 ON SFPRODAC_1.SFPRODDT_ID = SFPRODDT_1.ID LEFT OUTER JOIN
dbo.SFISSUHD AS SFISSUHD_1 LEFT OUTER JOIN
dbo.SFISSULT AS SFISSULT_1 ON SFISSUHD_1.id = SFISSULT_1.SFISSUHD_ID RIGHT OUTER JOIN
dbo.SFISSUDT AS SFISSUDT_1 ON SFISSUHD_1.id = SFISSUDT_1.SFISSUHD_ID ON
SFPRODDT_1.ID = SFISSUDT_1.In_SFPRODDT_ID RIGHT OUTER JOIN
dbo.SFPRODHD AS SFPRODHD_1 ON SFPRODDT_1.SFPRODHD_ID = SFPRODHD_1.ID RIGHT OUTER JOIN
dbo.SFJOBODT AS SFJOBODT_1 INNER JOIN
dbo.SFJOBOHD AS SFJOBOHD_1 ON SFJOBODT_1.SFJOBOHD_ID = SFJOBOHD_1.ID INNER JOIN
dbo.SASALEDT AS SASALEDT_1 ON SFJOBODT_1.SASALEDT_ID = SASALEDT_1.ID INNER JOIN
dbo.SASALEHD AS SASALEHD_1 ON SASALEDT_1.SASALEHD_ID = SASALEHD_1.ID INNER JOIN
dbo.SMBOMMHD AS SMBOMMHD_1 ON SASALEDT_1.SMBOMMHD_ID = SMBOMMHD_1.ID ON SFPRODHD_1.SFJOBOHD_ID = SFJOBOHD_1.ID
GROUP BY SMBOMMHD_1.Code, SASALEHD_1.Ref_Doc2
HAVING (NOT (MAX(SFJOBOHD_1.Job_No) LIKE '%AF%')) AND (NOT (MAX(SFJOBOHD_1.Job_No) LIKE '%SL%'))
ORDER BY Job_ID, Job_No, BOM, Op_No) AS tb2 ON tb1.Job_ID = tb2.Job_ID AND tb1.Op_No = tb2.Op_No";
$query = sqlsrv_query($serverConn,$sql);
$row = sqlsrv_fetch_array($query);
echo $row['tb1.Job_ID'];