2012-07-04 57 views
5

我想僅檢索10個記錄在德比數據庫中。我一直在尋找的教程這樣做:SQL爲限制記錄組在德比數據庫中

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當你把結果作爲內部查詢再次查詢,但德比的要求是使用row_number。首先在內部添加row_number,並在下一層的哪裏使用它。任何人都可以幫忙?德比不支持限制。首先我可以先訂購10首唱片的其他方式?

回答

9

我一直在研究,我發現這樣的 使用

FETCH FIRST 10 ROWS ONLY 

但可憐的我在德比限制在德比10.5的支持和我使用derby 10.4捆綁glassfish 2.1 任何人也許可以幫助,如果我仍然使用10.4 cuz我太害怕遷移

+1

可以給你完整的查詢 –

1

你可以給order by子句中 「OVER()」

OVER(order by effot desc) AS rownum 
+2

沒有。德比不支持:http://db.apache.org/derby/docs/10.7/ref/rreffuncrownumber.html –

+0

對不起,這是行不通的 是不支持,我怕 其仍然錯誤 我是使用德比10.4 –