$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.SASALEDT 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%'))) AS tb2
ON tb1.BOM = tb2.BOM AND tb1.Op_No = tb2.Op_No AND tb1.Job_ID = tb2.Job_ID";
$query = sqlsrv_query($serverConn,$sql);
$row = sqlsrv_fetch_array($query );
echo $row['tb1.Job_ID'];