Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
SQL to pull most recent from each subgroup
Message
 
 
À
20/01/2006 21:23:25
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Versions des environnements
Visual FoxPro:
VFP 9
Database:
Visual FoxPro
Divers
Thread ID:
01089098
Message ID:
01089113
Vues:
36
>>Try
>>SELECT cCode, cDesc, dDate ;
>>	FROM mytable mt1 ;
>>	WHERE NOT EXISTS ( ;
>>		SELECT * FROM mytable mt2 ;
>>				WHERE mt2.cCode = mt1.cCode AND mt2.dDate > mt1.dDate) ;
>>	INTO CURSOR crsResult
>>
>>* or
>>
>>SELECT cCode, cDesc, dDate ;
>>	FROM mytable mt1 ;
>>	WHERE dDate = ( SELECT MAX(dDate) FROM mytable mt2 ;
>>					WHERE mt2.cCode = mt1.cCode) ;
>>	INTO CURSOR crsResult
>>
>>>
>
>I've found that, for large tables, it may be significantly faster to split this into two queries.
>
SELECT cCode,MAX(dDate) as max_date FROM myTable ;
>   GROUP BY cCode;
>   INTO CURSOR curMaxDate
>
>SELECT mt.cCode,mt.cDesc,mt.dDate;
>  FROM myTable mt ;
>  JOIN curMaxDate md ON   mt.cCode = md.cCode ;
>    AND mt.dDate = md.max_date
>
>

It could be faster, slower or the same. Only tests on real data can tell.
--sb--
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform