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:
00243201
Views:
24
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.
>
>
>>Try this:
>>
>>SELECT CustId, MAX(total_price) AS maxprice FROM yourtable INTO CURSOR curs1 GROUP BY CustId
>>
>>SELECT curs1.CustId, yourtable.OrderNumber, curs1.maxprice) FROM yourtable INNER JOIN curs1 ON yourtable.CustID = curs1.CustID AND yourtable.total_price = curs1.maxprice INTO CURSOR endresult ORDER BY CustId, OrderNumber
>>
>>If Max(total_price) equals the price of more than one order_number for a given Cust_id, you'll get both order_numbers.
>>
>>>Oops. First() and Last() are aggregate functions available in Access 97 but apparently not in vfp6. Since I have done most of my aggregating in Access, I often think about SQL in Access terms. I'll have to think about this one a bit more. I have a feeling that the answer will involve more queries, one of which might number the records with RECNO(). There might be different queries that return Max(total_price) and order_number, which are then joined somehow. The answer will need to take into account the possibility that Max(total_price) may equal the price of more than one order_number for a given Cust_id.
>>>
>>>Or did you say that it was working now? If it is (and I don't see how) could you tell us what your SQL looks like?
>>>
>>>What is "berraquera"?
>>>
>>>>Thanks for your help. It worked with the two separated queries, but without the "LAST" function you suggest. What is that function? I did not even find documentation about it and got an error ("too many arguments") when i try to use it.
>>>>
>>>>Thanks again.
>>>>
>>>>>>Is it possible to get the maximum value from a field and other fields corresponding to the same record?
>>>>>>
>>>>>>i.e: Given a table with OrderNumber, CustId and total_Price, How do I get the highest total_price for every CustId and the Corresponding OrderNumber in a single query?
>>>>>>
>>>>>>I´ve tried something like:
>>>>>>Select CustId, OrderNumber,max(total_price) from [Tablename] group by CustId
>>>>>>
>>>>>>It returned the maximum value of "total_price" for every CustId, but the "OrderNumber" returned is not correct, given that in the source table the total_price for that Order_Number is not the same.
>>>>>>
>>>>>>Tks
>>>>>
>>>>>You might need two queries - one to order the records and another to give you your MAX.
>>>>>
>>>>>SELECT CustId, OrderNumber, total_price FROM yourtable INTO CURSOR curs1 ORDER BY CustId, total_price
>>>>>
>>>>>SELECT CustId, LAST(OrderNumber), LAST(total_price) FROM curs1 INTO CURSOR endresult GROUP BY CustId
>>>>>
>>>>>In the second query, LAST(total_price) and MAX(total_price) should give you the same thing.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform