Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How to get a Top n dataset grouped.
Message
 
To
18/07/2002 14:17:00
General information
Forum:
Visual FoxPro
Category:
Databases,Tables, Views, Indexing and SQL syntax
Miscellaneous
Thread ID:
00680116
Message ID:
00681534
Views:
22
>Hi everybody!,
>
> Is there a way to get with a sql statement the first top n rows of every filtered group?.Let me explain a little bit. I have a table with next fields:
>
> LotNumber,Container,Lot_Pieces,Lot_Balance
> 40134456,10101011,1000,80
> 40134457,10101011,1000,60
> 40334458,10101011,1000,20
> 40344458,20101011,1020,1020
> 40134558,20101011,400,10
> 40134758,20101011,800,759
> 40134858,20101011,1500,30
> 40134958,20101011,1500,30
>..
>..
> 50134458,80101011,400,400
> 60134455,80101011,400,300
> 70133458,80101011,800,5
> 8013438,80101011,320,20
> 90134488,80101011,2500,2
>
>
>I will like to get the first 3 records of every container that meets the next rule: Lot_balance<100
>Data Result
> 40134456,10101011,1000,80
> 40134457,10101011,1000,60
> 40334458,10101011,1000,20
>
> 40134558,20101011,400,10
> 40134858,20101011,1500,30
> 40134958,20101011,1500,30
>
> 70133458,80101011,800,5
> 8013438,80101011,320,20
> 90134488,80101011,2500,2
>
>Thanks in advance

Hi
I'm afraid it will become too complcated and slow-running query but you caould try somethink like this:
 SELECT t.LotNumber, t.Container, t.Lot_Pieces, t.Lot_Balance FROM MyTable t
     WHERE t.LotNumber IN (SELECT top 3 sq.LotNumber, sq.Container, sq.Lot_Balance 
                          FROM MyTable sq
                          WHERE sq.Container = t.Container )
Venelina Jordanova

Outsourcing IT Services Ltd.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform