Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic Top N by group

Status
Not open for further replies.

RJL1

Technical User
Oct 3, 2002
228
0
0
US
Hello

I have a new project. I have a table with customer number and a numeric value that represents the number of record I need to retrieve. For example

Code:
Customer      Records
042           10
317           25

I want to be able to run my query against my item master table and get 10 unique items for customer 042 and 25 unique items for customer 317. If the values for the record were the same no problem but I am not sure how to approach the variable nature of the record count

Thanks in advance.
Let me know if I need to detail this further
 
just one approach - code will not work as is but you should get the idea

select *
from (select customer
, row_number() over(partition by customer
) as rownum
from mycustomer
) t
inner join mymaster t1
on t1.customer = t.customer
and t.rownum <= t.records_to_read

Regards

Frederico Fonseca
SysSoft Integrated Ltd

FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top