Jun 29 2022 07:36 AM
Experts, I am building an update query.
I need to update tblFXParent to the MAX value of tblFXRollsChild].[DateSettle] where tblFXParent.IDFXParent=tblFXRollsChild.IDParentfk
I am getting an "aggregate function" error as below.
What am I doing wrong?
here is the SQL:
UPDATE tblFXParent INNER JOIN tblFXRollsChild ON tblFXParent.IDFXParent = tblFXRollsChild.IDParentfk SET tblFXParent.DateSettleC = Max([tblFXRollsChild].[DateSettle])
WHERE (([IDFXParent]=[IDParentfk]));
Jun 29 2022 10:05 AM
Actually, I think there are a couple of different kinds of problems.
First, attempting to update the field this way is not an appropriate approach in an Access relational database application. We do not recommend storing calculated values, such as this. For one thing, they run the risk of going out of synch because there's nothing to prevent a user from adding a new record in the tblFXRollsChild with a later date, which may or may not get updated again in the parent table. Risk of data integrity problems is one reason we seldom store calculated values.
Second, it can't be done with this syntax. I'm hesitating between saying, "just don't do it", and offering an example anyway.
Jun 29 2022 12:20 PM
Jun 29 2022 01:03 PM
Typically, this is done with a subquery to find the Max(Value) in the child table. Here that would be the Max date for each instance of IDParentfk, which links the records to the IDFXParent Primary Key:
SELECT IDParentfk, Max(DateSettle) AS LastSettlementDate
FROM tblFXRollsChild
GROUP BY IDParentfk
Then you can join this as a subquery in a SELECT query on the Parent table.
SELECT IDFXParent, LastSettlementDate
FROM tblFXParent INNER JOIN (
SELECT IDParentfk, Max(DateSettle) AS LastSettlementDate
FROM tblFXRollsChild
GROUP BY IDParentfk) AS MaxDates ON tblFXParent.IDFXParent = MaxDate.IDParentfk
ORDER BY IDFXParent
Jun 29 2022 05:58 PM
HI George, thank you very much for the guidance. Queries alone are hard enough and queries in queries are diabolically confusing to me but I think I can do it.
Question:
I have a field named tblFXParent.[DateSettleC].
Is LastSettlementDate to be this field? Meaning that I should change LastSettlementDate to DateSettleC? I assume this is the case but wanted to make sure.
Below is my sql if it helps.
All fields are denoted by the * so tblFXParent.[DateSettleC] is not showing. Note that I have 2 fields similarly named: tblFXParent.[DateSettleC] and tblFXParent.DateSettle (no "C")
SELECT tblFXParent.*, [rate]*[AmountOrig] AS USD, Nz([AmountOrig],0)+Nz(DSum("AmountRoll","tblFXRollsChild","[IDParentfk]=" & [IDFXParent]),0) AS [Balance EURO],
FROM tblFXParent
WHERE (((tblFXParent.AmountOrig) Like "*" & [Forms]![frmFXTracker]![txEndUser] & "*"))
ORDER BY tblFXParent.Vendor, tblFXParent.DateSettle;
Jun 29 2022 07:12 PM
SolutionJun 30 2022 06:54 AM
Jun 30 2022 06:40 PM
Jun 30 2022 08:03 PM
Jul 01 2022 04:23 AM - edited Jul 01 2022 04:24 AM
George, please see SQL below.
I have also attached a sample db with both tables and the query. You can see that if you change the Date tblRollsChild.DateSettle (where tblFXParent.[IDFXParent] = tblRollsChild.[IDParentfk]) then the qryMaxDtSettle.LastSettlementDate changes to NULL but if you change it back to that date you changed it from then the NULL changes back to what it was before. Basically, if the date is changed, it updates to NULL. I hope that makes sense.
thank you very much.
here is the SQL (but it is also in the attached db)
SELECT tblFXParent.*, [rate]*[AmountOrig] AS USD, Nz([AmountOrig],0)+Nz(DSum("AmountRoll","tblFXRollsChild","[IDParentfk]=" & [IDFXParent]),0) AS [Balance EURO], MaxDateSettle.LastSettlementDate
FROM tblFXParent LEFT JOIN (SELECT IDParentfk, Max(DateSettle) AS LastSettlementDate FROM tblFXRollsChild GROUP BY IDParentfk) AS MaxDateSettle ON (tblFXParent.[DateSettleC] = MaxDateSettle.[LastSettlementDate]) AND (tblFXParent.[IDFXParent] = MaxDateSettle.[IDParentfk])
ORDER BY tblFXParent.Vendor, tblFXParent.DateSettle;
Jul 01 2022 06:29 AM
Why did you use a LEFT OUTER JOIN?
I thought you only wanted to see the MAX of the settlement dates in the child table?
Jul 01 2022 06:33 AM
Ah, I think I see now. You only have settlement dates for SOME of the parents. This query with the LEFT OUTER JOIN is intended to return ALL of the Parents, but with the max settlement date IF there is one for that parent in the Child table. So, this is more complicated. Give me a bit. Also, with this data, I'm sure someone else will be joining us...
Jul 01 2022 06:51 AM
Well, if I am interpreting the requirement correctly, this version contains a modification to your query that should be very close to what you need, with one problem remaining.
It is a union of the SQL you had, except that it uses an INNER JOIN to match ONLY the max settlement date with the parent date, with a second SQL statement to return all of the other parent records which do NOT have a matching record in the child table.
There is one record in the child table which appears to be a bogus record, or a record that doesn't match what we know so far about the tables.
If this is a legit record, we can make one further modification to the SQL to include it in the results. That's the second query in the attached.
Jul 01 2022 09:02 AM
Hi George,
thank you for the revised.
I still seem to have that issue with the record though.
If I change the tblFXRollsChild.DateSettle the qryMaxDtSettle as well as the other query will completely remove that ID but if I change it back then it reappears. Not sure if you see the same on your end?
Bogus Record:
Its not really a bogus record. I entered it but then backtracked but left the record since I know it will be populated very soon with data. It was a notification to self (I removed all of the comments and if I left them then it might have made more sense why the record was there.
I think this bogus type record will appear again though as I typically enter a record but the date is NULL.
==>I am am not sure if the calculation would need to be adjusted if there is a NULL for the date. I assume not since I am needing the MAX.
Jul 01 2022 09:12 AM
I added a second query to handle that one record, it might not have made it into the zip file:
SELECT tblFXParent.*, [rate]*[AmountOrig] AS USD, Nz([AmountOrig],0)+Nz(DSum("AmountRoll","tblFXRollsChild","[IDParentfk]=" & [IDFXParent]),0) AS [Balance EURO], MaxDateSettle.LastSettlementDate
FROM tblFXParent INNER JOIN (SELECT IDParentfk, Max(DateSettle) AS LastSettlementDate FROM tblFXRollsChild GROUP BY IDParentfk) AS MaxDateSettle ON (tblFXParent.[DateSettleC] = MaxDateSettle.[LastSettlementDate]) AND (tblFXParent.[IDFXParent] = MaxDateSettle.[IDParentfk])
UNION SELECT tblFXParent.*, [rate]*[AmountOrig] AS USD, Nz([AmountOrig],0)+Nz(DSum("AmountRoll","tblFXRollsChild","[IDParentfk]=" & [IDFXParent]),0) AS [Balance EURO], tblFXParent.IDFXParent
FROM tblFXParent
WHERE (((tblFXParent.IDFXParent) Not In (SELECT IDParentfk From tblFXRollsChild)))
UNION SELECT tblFXParent.*, [rate]*[AmountOrig] AS USD, Nz([AmountOrig],0)+Nz(DSum("AmountRoll","tblFXRollsChild","[IDParentfk]=" & [IDFXParent]),0) AS [Balance EURO], tblFXParent.IDFXParent
FROM tblFXParent
WHERE (((tblFXParent.IDFXParent) In (SELECT IDParentfk From tblFXRollsChild WHERE DateSettle IS NULL)))
ORDER BY Vendor, DateSettle;
Jul 01 2022 10:41 AM
George, I still have that issue. Just to be clear, I am referring to when I change the date that record disappears but when I change it back then it reappears (see comment in my previous note).
In the query above, I pasted into qryMaxDtSettle.
Let me know if yo8u are able to reproduce on your end.
Also, the the ID is being populated in the LastSettlement date field its seems?
Jul 01 2022 01:17 PM - edited Jul 01 2022 01:18 PM
That could happen if the fields in the UNION query are not properly aligned. The exact same fields must be in the same position in each SQL Statement within the query, and there must be exactly the same fields. I see that I did overlook the presence of the additional field from the subquery for the MaxDateSettle.LastSettlementDate field because I had included an additional field for the foreign key in exactly that same position. I apologize for not analyzing the output properly. I was primarily looking at record count. Amend it like this.
SELECT tblfxparent.*,
[rate] * [amountorig] AS USD,
Nz([amountorig], 0)
+ Nz(Dsum("amountroll", "tblfxrollschild", "[idparentfk]=" & [idfxparent]), 0) AS [Balance EURO],
MaxDateSettle.lastsettlementdate
FROM tblfxparent
INNER JOIN (SELECT idparentfk,
Max(datesettle) AS LastSettlementDate
FROM tblfxrollschild
GROUP BY idparentfk) AS MaxDateSettle
ON ( tblfxparent.[datesettlec] =
MaxDateSettle.[lastsettlementdate] )
AND ( tblfxparent.[idfxparent] = MaxDateSettle.[idparentfk] )
UNION
SELECT tblfxparent.*,
[rate] * [amountorig] AS USD,
Nz([amountorig], 0)
+ Nz(Dsum("amountroll", "tblfxrollschild", "[idparentfk]=" & [idfxparent] ), 0) AS [Balance EURO],
Null AS lastsettlementdate
FROM tblfxparent
WHERE (( ( tblfxparent.idfxparent ) NOT IN (SELECT idparentfk
FROM tblfxrollschild) ))
UNION
SELECT tblfxparent.*,
[rate] * [amountorig] AS USD,
Nz([amountorig], 0)
+ Nz(Dsum("amountroll", "tblfxrollschild", "[idparentfk]=" & [idfxparent]), 0) AS [Balance EURO],
Null AS lastsettlementdate
FROM tblfxparent
WHERE (( ( tblfxparent.idfxparent ) IN (SELECT idparentfk
FROM tblfxrollschild
WHERE datesettle IS NULL) ))
ORDER BY vendor,
datesettle;
The problem with date changes will have to wait I'm afraid. I've got a meeting starting in a few minutes.
Jul 01 2022 01:49 PM
Jul 01 2022 05:10 PM
George, That query looks very intimidating. Definitely expert level.
Do you think that the relationship might have something to do with the record disappearing? thank you for your availability and kindness. Anytime is fine with me.
GROUP BY idparentfk) AS MaxDateSettle
ON ( tblfxparent.[datesettlec] =
MaxDateSettle.[lastsettlementdate] )
AND ( tblfxparent.[idfxparent] = MaxDateSettle.[idparentfk] )
Jul 02 2022 06:02 AM