Db2 - 返回每个类别的前5名 [英] Db2 - Returning the top 5 of each category

查看:144
本文介绍了Db2 - 返回每个类别的前5名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要能够每个菜单返回5个菜单。
这里是表

 
菜单
-------
menuid int()
profileName varchar(35)

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

我在这个线程中看到了MySQL的解决方案 - mySQL返回每个类别的前5个,寻找类似于DB2的解决方案。
任何建议是非常感谢。

解决方案

如下:

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

其中rn <= 5;

Arvin有不同DBMS的比较:

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

另外还有一些配额查询(比如你所要求的)被讨论


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.

解决方案

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

这篇关于Db2 - 返回每个类别的前5名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆