Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
SQL max() and corresponding fields
Message
General information
Forum:
Visual FoxPro
Category:
Other
Miscellaneous
Thread ID:
00241387
Message ID:
00243415
Views:
21
Select * from MyTable order by cust_id,total_price into cursor result_1

result_1 is something like:
cust_id total_price Order_no
1 100 75
1 150 45
1 150 80
2 90 15
2 110 12

Then: Select cust_id, order_no, MAX(Total_price) as TP from result_1 Group by cust_id into cursor result_2

results in
cust_id total_price Order_no
1 150 80
2 110 12

This demonstrates that when SQL (at least Vfp-sql) GROUPS, it returns the last record processed for every group.

Also note that only 1 record for a customer is selected even if there are two records with the same Maximum value

>One again my SQL thinking is Access-affected. I work on one major vfp project and one major Access project. The vfp data is not numeric, but the Access data is manhours from different sources, which has to be summed and grouped every which way but loose. I would like to see the SQL that works, to be sure that I understand what you are saying. OrderNumber doesn't have to be in the GROUP BY clause, and it doesn't have to be used in an aggregate function like MAX() either? Vfp is smart enough to give you the corresponding value? Access would give you an error. But it makes up for that by giving you more aggregate functions.
>
>>It seems vfp works like this:
>>
>>When GROUP BY clause is used, SQL returns the values for the last record evaluated in every group; So if I GROUP and MAX on an ordered cursor, it returns the Maximum value and the corresponding values for every other field, as you first suggest, the key is to ORDER and then MAX on the ordered cursor.
>>
>>"Berraquera": something very good, very strong; something powerful.
>>
>>Juan Montoya (Leader of Formula Cart): He´s from Colombia, and He´s "La berraquera" too.
>>
>>
There´s a better life... Just it´s more expensive!
Cristóbal Manrique M.
Previous
Reply
Map
View

Click here to load this message in the networking platform