Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Using select top N clause
Message
 
À
01/02/2008 09:59:11
Information générale
Forum:
Visual FoxPro
Catégorie:
Base de données, Tables, Vues, Index et syntaxe SQL
Versions des environnements
Visual FoxPro:
VFP 9 SP1
OS:
Windows XP SP2
Network:
Windows XP
Database:
Visual FoxPro
Divers
Thread ID:
01287704
Message ID:
01288294
Vues:
13
>>Hi All,
>>i've custNotes table and wantto query last 3 notes from each customer from custNotes table , below codes doesn't solve my need :(
>>
>>where is the problem ?
>>
>>
>>SELECT TOP 3 date,custId,notes FROM custNotes ORDER BY date,custNotes DESC
>>
>
>If you want it for each customer, TOP 3 isn't going to do it for you. The solution I know comes from Fabio Lunardon and does like this:
>
>
>SELECT CustNotes.Date, CustNotes.CustID, CustNotes.Notes ;
>   FROM CustNotes ;
>     JOIN (SELECT cna.Date, cna.CustID ;
>             FROM CustNotes cna ;
>               JOIN CustNotes cnb ;
>                 ON cna.CustID = cnb.CustID ;
>                 AND cna.Date <= cnb.Date ;
>             GROUP BY 1, 2 ;
>             HAVING CNT(*) <= 3 ) MostRecent ;
>       ON CustNotes.Date = MostRecent.Date ;
>       AND CustNotes.CustID = MostRecent.CustID ;
>  INTO CURSOR MostRecentNotes
>
>
>The inner query finds the three most recent records for each customer and the outer then pulls the data you need. Note that if you could have more than one record per customer per date, you'll need to find a way to distinguish them.
>
>Tamar

Thanks Tamar it working fine :)
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform