Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Using select top N clause
Message
 
 
To
01/02/2008 09:59:11
General information
Forum:
Visual FoxPro
Category:
Databases,Tables, Views, Indexing and SQL syntax
Environment versions
Visual FoxPro:
VFP 9 SP1
OS:
Windows XP SP2
Network:
Windows XP
Database:
Visual FoxPro
Miscellaneous
Thread ID:
01287704
Message ID:
01288294
Views:
12
>>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 :)
Previous
Reply
Map
View

Click here to load this message in the networking platform