$str_condition="
FROM tblSlave_SN_History AS tssh
INNER JOIN
(
SELECT DISTINCT tssh2.Serial_Number_ID
FROM tblSlave_SN_History AS tssh2
INNER JOIN tblLot_Number AS tln ON tln.Lot_Number_ID=tssh2.Lot_Number_ID
INNER JOIN tblProduct_Model AS tpm ON tpm.Product_Model_ID=tln.Product_Model_ID
INNER JOIN tblProduct AS tpd ON tpd.Product_ID=tpm.Product_ID
WHERE tssh2.Station_ID=".$pbl_int_armheight_station." AND
tssh2.Record_Time BETWEEN '".$str_from_date1."' AND '".$str_from_date2."'";
if($_POST["cboCustomer"]>0){
$str_condition.=" AND tpd.Customer_ID=".$_POST["cboCustomer"];
}
if($_POST["cboProduct"]>0)
$str_condition.=" AND tpd.Product_ID=".$_POST["cboProduct"];
if($_POST["cboLotType"]>0)
$str_condition.=" AND tln.Lot_Type_ID=".$_POST["cboLotType"];
$str_condition.="
) AS qtemp ON qtemp.Serial_Number_ID=tssh.Serial_Number_ID
LEFT JOIN tblMaster_SN_History AS tmsh ON tmsh.Master_SN_History_ID=tssh.Master_SN_History_ID
WHERE tssh.Record_Time >= '".$str_from_date1."'
GROUP BY tmsh.Comp
HAVING
SUM( CASE WHEN tssh.Station_ID=".$pbl_int_armheight_station."
THEN 1 ELSE 0 END)>0
ORDER BY tmsh.Comp
";
//,COUNT(DISTINCT tssh.Serial_Number_ID) AS Tested_Qty
$sql="SELECT tmsh.Comp
,SUM( CASE WHEN tssh.Result=1 AND tssh.Station_ID=".$pbl_int_armheight_station." THEN 1 ELSE 0 END ) AS Test_Pass_Qty
,SUM( CASE WHEN tssh.Result=0 AND tssh.Station_ID=".$pbl_int_armheight_station." THEN 1 ELSE 0 END ) AS Test_Fail_Qty
,SUM( CASE WHEN tssh.Result=1 AND tssh.Station_ID=".$pbl_int_confirm_station." THEN 1 ELSE 0 END ) AS Confirm_Pass_Qty
,SUM( CASE WHEN tssh.Result=0 AND tssh.Station_ID=".$pbl_int_confirm_station." THEN 1 ELSE 0 END ) AS Confirm_Fail_Qty
";