B
BruceM
After some discussion and very helpful suggestions in this forum I have put
together SQL that finds the highest value in the PO_Rev field for a given
PO_Number.
By way of background, ReqID is the PK. PO_Number is assigned after
approvals are complete. A PO can be revised, in which case a new record is
created with the same PO_Number and a new PO_Rev. In the case of a revision
the number exists before the approvals are complete.
Here is the abbreviated SQL (leaving out fields that are not relevant to the
question at hand):
SELECT P.PO_Number, P.ReqID, P.PO_Rev,
P.SupplierID, P.AdminApp, P.ProdApp, S.SupplierName
FROM tblSupplier AS S
INNER JOIN tblPO AS P
ON S.SupplierID = P.SupplierID
WHERE P.PO_Rev = (SELECT Max(P2.PO_Rev)
FROM tblPO AS P2
WHERE PO_Number = P2.PO_Number
AND P.AdminApp Is Null)
OR P.PO_Rev=(SELECT Max(P2.PO_Rev)
FROM tblPO AS P2
WHERE P.PO_Number = P2.PO_Number
AND P.ProdApp Is Null)
ORDER BY P.PO_Number, P.ReqID;
My question is about this part of the SQL, which returns only the highest
PO_Rev for a record in which AdminApp is null:
(SELECT Max(P2.PO_Rev) _
FROM tblPO AS P2 _
WHERE PO_Number = P2.PO_Number _
AND P.AdminApp Is Null)
I wanted to test whether AdminApp or ProdApp are null, so I tried this:
(SELECT Max(P2.PO_Rev) _
FROM tblPO AS P2 _
WHERE PO_Number = P2.PO_Number _
AND (P.AdminApp Is Null
OR P.ProdApp Is Null))
However, this introduced into the recordset an earlier revision of one of
the POs. That revision had not been fully approved (i.e. AdminApp was
null). In order to eliminate that record I had to repeat the entire
subquery (if I am using the correct term) after the OR, but with ProdApp
instead of AdminApp. That is the first SQL above, which produces the
intended results.
Can I consolidate the SQL along the lines I hoped, or do I need to repeat
the whole thing?
BTW, I need to stress that this leaves out a lot of details. Questions may
arise about why I am storing the PO_Number in two different records in cases
where there is a revision. I can explain more fully, but I fear it would
unnecessarily complicate the question that I am trying to define as narrowly
as is possible.
together SQL that finds the highest value in the PO_Rev field for a given
PO_Number.
By way of background, ReqID is the PK. PO_Number is assigned after
approvals are complete. A PO can be revised, in which case a new record is
created with the same PO_Number and a new PO_Rev. In the case of a revision
the number exists before the approvals are complete.
Here is the abbreviated SQL (leaving out fields that are not relevant to the
question at hand):
SELECT P.PO_Number, P.ReqID, P.PO_Rev,
P.SupplierID, P.AdminApp, P.ProdApp, S.SupplierName
FROM tblSupplier AS S
INNER JOIN tblPO AS P
ON S.SupplierID = P.SupplierID
WHERE P.PO_Rev = (SELECT Max(P2.PO_Rev)
FROM tblPO AS P2
WHERE PO_Number = P2.PO_Number
AND P.AdminApp Is Null)
OR P.PO_Rev=(SELECT Max(P2.PO_Rev)
FROM tblPO AS P2
WHERE P.PO_Number = P2.PO_Number
AND P.ProdApp Is Null)
ORDER BY P.PO_Number, P.ReqID;
My question is about this part of the SQL, which returns only the highest
PO_Rev for a record in which AdminApp is null:
(SELECT Max(P2.PO_Rev) _
FROM tblPO AS P2 _
WHERE PO_Number = P2.PO_Number _
AND P.AdminApp Is Null)
I wanted to test whether AdminApp or ProdApp are null, so I tried this:
(SELECT Max(P2.PO_Rev) _
FROM tblPO AS P2 _
WHERE PO_Number = P2.PO_Number _
AND (P.AdminApp Is Null
OR P.ProdApp Is Null))
However, this introduced into the recordset an earlier revision of one of
the POs. That revision had not been fully approved (i.e. AdminApp was
null). In order to eliminate that record I had to repeat the entire
subquery (if I am using the correct term) after the OR, but with ProdApp
instead of AdminApp. That is the first SQL above, which produces the
intended results.
Can I consolidate the SQL along the lines I hoped, or do I need to repeat
the whole thing?
BTW, I need to stress that this leaves out a lot of details. Questions may
arise about why I am storing the PO_Number in two different records in cases
where there is a revision. I can explain more fully, but I fear it would
unnecessarily complicate the question that I am trying to define as narrowly
as is possible.