B
BruceM
This should be basic enough, but I can't figure it out. I have a Purchase
Order database of typical design that includes a Purchase Order table
(tblPO) and a Line Items table (tblLines), with one PO >> many line items.
In some cases a PO may be revised. The defualt value in this field (PO_Rev)
is 0. When a revised PO is created, the value is 1, then 2, etc.; it never
gets beyond maybe 3 revisions.
I want the recordset to include only the highest PO_Rev for each PO. There
is a wrinkle (maybe) in that when an order is created it is a requisition.
ReqID is the PK for the table. The PO Number (PO_Number) is created only
after the PO has been approved (approval field not shown). Until then it is
null (but could be 0, if that helps). Sample raw data:
ReqID PO_Number PO_Rev
10 1245 0
12 1247 0
13 1247 1
15 0
16 0
Desired recordset:
ReqID PO_Number PO_Rev
10 1245 0
13 1247 1
15 0
16 0
This is the SQL with subquery I attempted, but it returns the entire
recordset:
SELECT tblPO.ReqID, tblPO.PO_Number, tblPO.PO_Rev,
(SELECT TOP 1 P2.PO_Rev
FROM tblPO AS P2
WHERE tblPO.PO_Number = P2.PO_Number
ORDER BY P2.PO_Rev DESC) AS OnePO
FROM tblPO;
However, even if I can get it to work, it seems the recordset is not
editable, so maybe this is not the correct course after all.
It could be I need to add a Boolean field to tblPO for CurrentRev. When a
new revision is created, the previous revision has CurrentRev marked as
False. I could then use True in CurrentRev as the criteria in most cases.
Actually, I wouls use Allen Browne's substitute Yes/No field instead of a
Boolean, but the idea is the same in any case. However, since the recordset
seems to contain all the data I need it seems adding this extra field should
not be necessary, but I haven't figured out the alternative yet.
Another thing I tried was grouping:
SELECT Max(PO_Rev) as MaxRev
FROM tblPO
GROUP BY Nz(PO_Number,ReqID)
This returns the right number of records. The trouble is I am unsure how to
add other fields to a totals query. In order to join this query to tblPO I
added ReqID to the SELECT part of the query, but since it is a grouping
query I had to do something with the field, so I tried Sum, and ended up
with this when I joined to tblPO:
SELECT tblPO.*
FROM tblPO
RIGHT JOIN qryOnePO
ON tblPO.ReqID = qryOnePO.SumOfReqID;
This works, but I am concerned about the use of Sum.
Order database of typical design that includes a Purchase Order table
(tblPO) and a Line Items table (tblLines), with one PO >> many line items.
In some cases a PO may be revised. The defualt value in this field (PO_Rev)
is 0. When a revised PO is created, the value is 1, then 2, etc.; it never
gets beyond maybe 3 revisions.
I want the recordset to include only the highest PO_Rev for each PO. There
is a wrinkle (maybe) in that when an order is created it is a requisition.
ReqID is the PK for the table. The PO Number (PO_Number) is created only
after the PO has been approved (approval field not shown). Until then it is
null (but could be 0, if that helps). Sample raw data:
ReqID PO_Number PO_Rev
10 1245 0
12 1247 0
13 1247 1
15 0
16 0
Desired recordset:
ReqID PO_Number PO_Rev
10 1245 0
13 1247 1
15 0
16 0
This is the SQL with subquery I attempted, but it returns the entire
recordset:
SELECT tblPO.ReqID, tblPO.PO_Number, tblPO.PO_Rev,
(SELECT TOP 1 P2.PO_Rev
FROM tblPO AS P2
WHERE tblPO.PO_Number = P2.PO_Number
ORDER BY P2.PO_Rev DESC) AS OnePO
FROM tblPO;
However, even if I can get it to work, it seems the recordset is not
editable, so maybe this is not the correct course after all.
It could be I need to add a Boolean field to tblPO for CurrentRev. When a
new revision is created, the previous revision has CurrentRev marked as
False. I could then use True in CurrentRev as the criteria in most cases.
Actually, I wouls use Allen Browne's substitute Yes/No field instead of a
Boolean, but the idea is the same in any case. However, since the recordset
seems to contain all the data I need it seems adding this extra field should
not be necessary, but I haven't figured out the alternative yet.
Another thing I tried was grouping:
SELECT Max(PO_Rev) as MaxRev
FROM tblPO
GROUP BY Nz(PO_Number,ReqID)
This returns the right number of records. The trouble is I am unsure how to
add other fields to a totals query. In order to join this query to tblPO I
added ReqID to the SELECT part of the query, but since it is a grouping
query I had to do something with the field, so I tried Sum, and ended up
with this when I joined to tblPO:
SELECT tblPO.*
FROM tblPO
RIGHT JOIN qryOnePO
ON tblPO.ReqID = qryOnePO.SumOfReqID;
This works, but I am concerned about the use of Sum.