子查询
SELECT ISNULL(COUNT(A.PONo), 0)
FROM (
SELECT PM.PONo
,(
SELECT SUM(TotalQty)
FROM GRN_Details GD
INNER JOIN GRN_Master GM ON GD.GRNNo = GM.GRNNo
WHERE PONo = PM.PONo
GROUP BY PONo
) [GRN_Qty]
,(
SELECT SUM(qty)
FROM PODetails PD
WHERE RevisionNo = (
SELECT MAX(RevisionNo)
FROM PODetails
WHERE PONo = PD.PONo
)
GROUP BY PD.PONo
) [PO_Qty]
FROM POMaster PM
WHERE RevisionNo = (
SELECT Isnull(max(RevisionNo), 0)
FROM POMaster
WHERE PONo = PM.PONo
)
AND CONVERT(DATE, DocumentDate) = GETDATE()
AND IsCancel = 0
AND IsApproved = 1
) A
WHERE A.GRN_Qty < A.PO_Qty