Db2 - Returning the top 5 of each category

别说谁变了你拦得住时间么 提交于 2019-11-28 12:25:21

问题


I want to be able to return 5 menuitem per menu. Here are the tables

menus
-------
menuid int()
profileName varchar(35)

menuitems
-----------
itemid int()
name varchar(40)
menuid int()

I do see a solution for MySQL in this thread - mySQL Returning the top 5 of each category, looking for similar solution for DB2. Any suggestion is great appreciated.


回答1:


something like:

select ...
from (
    select ..., row_number() over ( partition by m.menuid
                                    order by ? ) as rn 
    from menus m
    join menuitems mi
        m.menuid = mi.menuid
)
where rn <= 5;

Troels Arvin has a comparision of different DBMS at:

http://troels.arvin.dk/db/rdbms/

Amongst other things quota queries (such as the one you are asking) are discussed



来源:https://stackoverflow.com/questions/23802759/db2-returning-the-top-5-of-each-category

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!