On Thu, Jun 21, 2018 at 10:31 AM, Ken Dibble <[email protected]> wrote:
> Hi Folks,
>
> I have to admit that I've never been able to figure out how to get MAX() to
> work correctly in queries.
>
> Given the following tables (simplified):
>
> Consumers
>
> PK
> 1
> 2
> 3
>
> Status
>
> PK      ConFK           Date            Type
> 1       1               {^2018/1/15}    Pending
> 2       1               {^2018/4/15}    Approved
> 3       2               {^2018/6/1}     Closed
> 4       2               {^2018/4/15}    Approved
> 5       3               {^2017/11/22}   Pending
> 6       3               {^2018/2/3}     Approved
> 7       3               {^2018/3/12}    Suspended
> 8       3               {^2018/5/11}    Approved
>
> Hours
>
> PK      ConFK           Date            AppHours
> 1       2               {^2017/7/3}     10
> 2       2               {^2018/3/12}    14
> 3       3               {^2018/4/1/}    20
> 4       3               {^2018/5/1}     12
>
> Query: How many Consumers have a most recent Status of Approved, and what is
> the sum of those Consumers' Approved Hours?
>
> Answer: 2 Approved Consumers, having a total of 12 Hours.

Not at a machine with VFP, so I can't test this, but it looks good in
Google Mail :)

SELECT Consumer.*, Status.*, SUM(Hours.AppHours) AS TotalHours
FROM Consumer
JOIN Status on Consumer.ConPK=Status.ConFK
JOIN Hours on Consumer.ConPK=Hours.ConFK
WHERE Status.Date = (SELECT MAX(Stat2.Date) FROM Status Stat2 WHERE
Stat2.ConFK = Consumer.ConPK)
 AND Status.Type = "Approved"

-- 
Ted Roche
Ted Roche & Associates, LLC
http://www.tedroche.com

_______________________________________________
Post Messages to: [email protected]
Subscription Maintenance: http://mail.leafe.com/mailman/listinfo/profox
OT-free version of this list: http://mail.leafe.com/mailman/listinfo/profoxtech
Searchable Archive: http://leafe.com/archives/search/profox
This message: 
http://leafe.com/archives/byMID/profox/CACW6n4sOg5KjDvcTX5TRU7C2A3-XuP1yms=8bfybdmaiaef...@mail.gmail.com
** All postings, unless explicitly stated otherwise, are the opinions of the 
author, and do not constitute legal or medical advice. This statement is added 
to the messages for those lawyers who are too stupid to see the obvious.

Reply via email to