5

我只想在 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

我可以先使用 whererownum <= 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 并在下一层中使用它。任何人都可以帮忙吗?德比不受支持限制。有什么其他方法可以让我先订购 10 条记录吗?

4

2 回答 2

10

我一直在研究,我发现这样可以通过使用来限制德比

FETCH FIRST 10 ROWS ONLY

但是可怜的我,在 derby 10.5 中的支持,我正在使用 derby 10.4 捆绑包和 glassfish 2.1,如果我仍然使用 10.4,任何人都可以帮助解决这个问题,因为我太害怕迁移了

于 2012-07-05T06:50:49.053 回答
1

您可以在“OVER()”中给出 order by 子句

OVER(order by effot desc) AS rownum
于 2012-07-04T07:32:24.290 回答