sby for derby数据库中的限制记录组

时间:2012-07-04 07:21:51

标签: sql derby

我想只在derby数据库中检索十条记录。我一直在搜索教程:

select * from (select  ROW_NUMBER() OVER() AS rownum,name,effort from (select  name, effort from
(select TBL_PROJECT_DETAIL.P_NAME as Name, sum(TBL_TIMESHEET.EFFORT) as effort from
TBL_TIMESHEET join tbl_project_detail on TBL_TIMESHEET.PROJECT_ID = TBL_PROJECT_DETAIL.ID group by TBL_PROJECT_DETAIL.P_NAME) as New)
as new2)
as new3 
where rownum <= 10

我可以通过最后使用rownum <= 10的位置将数据限制为10,但问题是数据不是先用order by子句排序,所以我得到了10个随机数据。

当我将order by放在数据前rownum

select * from (select  ROW_NUMBER() OVER() AS rownum,name,effort from 
(select  name, effort from
(select TBL_PROJECT_DETAIL.P_NAME as Name, sum(TBL_TIMESHEET.EFFORT) as effort from
TBL_TIMESHEET join tbl_project_detail on TBL_TIMESHEET.PROJECT_ID = TBL_PROJECT_DETAIL.ID group by TBL_PROJECT_DETAIL.P_NAME)
as New)
as new2 order by effot desc)
as new3  where rownum <=10

它给出了错误。当你将结果再次作为内部表进行查询时,它就像group by一样,但对derby的要求是使用row_number。首先在内部添加row_number并在下一层中的where中使用它。任何人都可以帮忙吗?德比不是支持限制。我可以先订购10个记录的其他方式吗?

2 个答案:

答案 0 :(得分:10)

我一直在研究,我发现这样 使用

来限制德比
FETCH FIRST 10 ROWS ONLY

但是糟糕的我支持德比10.5并且我使用德比10.4捆绑与glassfish 2.1 任何人都可以帮助这个,如果我仍然使用10.4因为我太害怕迁移

答案 1 :(得分:1)

您可以在“OVER()”

中提供order by子句
OVER(order by effot desc) AS rownum