Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
GROUP BY on ORDER BY
Message
 
 
To
15/07/2000 15:03:11
General information
Forum:
Visual FoxPro
Category:
Databases,Tables, Views, Indexing and SQL syntax
Miscellaneous
Thread ID:
00392909
Message ID:
00392922
Views:
10
If no aggregate function ise used (e.g. SUM, COUNT) then VFP will return the value contained in the last record physically in the table that adheres to the GROUP BY clause. As Marek suggested, use the MAX function to return the highest date in the group:
SELECT MASTER.MASTERKEY,MAX(CHILD.DATE) AS CHILDDATE FROM CHILD;
   INNER JOIN MASTER ON CHILD.MASTERKEY=MASTER.MASTERKEY;
   GROUP BY CHILD.MASTERKEY ORDER BY CHILD.DATE DESCENDING;
   INTO CURSOR TEMP
This will return:

000001 01/01/2000
000002 01/01/2000

>I am doing a query on two tables. Multiple records are found in the child table based on the primary key of the master table. I need to only keep one record from the child table. That record is the one on top of the ORDER BY.
>
>If I don't include the GROUP BY, the record I want to keep is always at the top. However, as I only need to keep one record from the child record, I have to put a GROUP BY. When I put, it doesn't take the top record.
>
>SELECT MASTER.MASTERKEY,CHILD.DATE AS CHILDDATE FROM CHILD;
> INNER JOIN MASTER ON CHILD.MASTERKEY=MASTER.MASTERKEY;
> GROUP BY CHILD.MASTERKEY ORDER BY CHILD.DATE DESCENDING;
> INTO CURSOR TEMP
>
>If I don't put the GROUP BY, I'll have something like this:
>
>000001 01/01/2000
>000001 12/12/1999
>000001 05/06/1999
>000001 05/05/1999
>000002 01/01/2000
>000002 12/11/1999
>000002 10/10/1999
>000002 09/09/1999
>
>If I put the GROUP BY, I'll have something like this:
>
>000001 05/05/1999
>000002 09/09/1999
Larry Miller
MCSD
LWMiller3@verizon.net

Accumulate learning by study, understand what you learn by questioning. -- Mingjiao
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform