Hi,
I have an UPDATE query I hope to get some help with.
I have two tables, tblcustomers and tblreadings. The are related through on-to-many (tblcustomers -> tblreadings).
I would like the UPDATE to perform the following:
For every customer in tblcustomer, find the MAX(Reading) and corresponding ReadingDate, and add that to the MaxReading and MaxReadingDate field for that specific customer. I guess I need som GROUP BY and MAX on the tblreadings and the UPDATE, SET and JOIN that subquery to the tblcustomers, but I am unsure?
tblcustomers (simplyfied)
CustomerID - MaxReading - MaxReadingDate
------------------------------------------
1 - Null - Null
2 - Null - Null
tblreadings (simplyfied)
ReadingID - CustomerID - Reading - ReadingDate
-----------------------------------------------
1 - 1 - 200 - 2012-01-01
2 - 2 - 300 - 2012-02-02
3 - 1 - 400 - 2011-09-01
I have below given a short example of what I would like the result UPDATE to be:
tblcustomers
CustomerID - MaxReading - MaxReadingDate
------------------------------------------
1 - 400 - 2011-09-01
2 - 300 - 2012-02-02
Someone with an idea?
I have an UPDATE query I hope to get some help with.
I have two tables, tblcustomers and tblreadings. The are related through on-to-many (tblcustomers -> tblreadings).
I would like the UPDATE to perform the following:
For every customer in tblcustomer, find the MAX(Reading) and corresponding ReadingDate, and add that to the MaxReading and MaxReadingDate field for that specific customer. I guess I need som GROUP BY and MAX on the tblreadings and the UPDATE, SET and JOIN that subquery to the tblcustomers, but I am unsure?
tblcustomers (simplyfied)
CustomerID - MaxReading - MaxReadingDate
------------------------------------------
1 - Null - Null
2 - Null - Null
tblreadings (simplyfied)
ReadingID - CustomerID - Reading - ReadingDate
-----------------------------------------------
1 - 1 - 200 - 2012-01-01
2 - 2 - 300 - 2012-02-02
3 - 1 - 400 - 2011-09-01
I have below given a short example of what I would like the result UPDATE to be:
tblcustomers
CustomerID - MaxReading - MaxReadingDate
------------------------------------------
1 - 400 - 2011-09-01
2 - 300 - 2012-02-02
Someone with an idea?