Try to search your question here, if you can't find : Ask Any Question Now ?

Select target number of records by groups

HomeCategory: stackoverflowSelect target number of records by groups
sourav asked 2 weeks ago

I have a database in which each record has a rank and is associated to a certain group.
Also, there is a target number of IDs for each group.

I need to select this target number in each group with the highest ranked records.

This is an example of this data:

Group            Id      Rank
--------------------------------
GUADALAJARA       1       356
GUADALAJARA       2       387
PUEBLA            3       431
TIJUANA           4       315
PUEBLA            5       315
MONTERREY         6       315
MONTERREY         7       263
PUEBLA            8       356
PUEBLA            9       447
GUADALAJARA      10       356
MONTERREY        11       356
TIJUANA          12       447
PUEBLA           13       356
PUEBLA           14       387
MONTERREY        15       431
MONTERREY        16       412
MONTERREY        17       447
TIJUANA          18       263

And the targets for each group are:

Group        Records   Goal
----------------------------
GUADALAJARA     4       2
MONTERREY       6       3
PUEBLA          6       3
TIJUANA         3       2    

For example, group Guadalajara has 4 records , and I need to select the first two highest-ranked which would be 100630487 and 133255369:

ID          Rank
----------------
100630487   447
133255369   387
138314098   356
114194869   356

I will appreciate any ideas to make this query.

1 Answers
Best Answer
Jyoti answered 2 weeks ago
Your Answer

12 + 0 =

Popular Tags

WP Facebook Auto Publish Powered By : XYZScripts.com