adding limit and offset to an oracle query that uses partition over
本问题已经有最佳答案,请猛点这里访问。
Possible Duplicate:
How do I limit the number of rows returned by an oracle query?
我在Oracle中有这个查询,它使用一些元数据和线程讨论最后一张海报从数据库中获取所有讨论。我想在查询中添加限制和偏移量,如何更改它以支持它。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | SELECT * FROM (SELECT discussions.created_at, discussions.id, discussions.title, discussions.stub, users.username AS created_by, COUNT(DISTINCT threads.id) OVER (partition BY discussions.created_at, discussions.title, users.username) AS replies, COUNT(DISTINCT discussion_views.discussion_id) OVER (partition BY discussions.created_at, discussions.title, users.username) AS views, latest_poster.username AS latest_post_by, threads.updated_at AS latest_post_at, ROW_NUMBER() OVER (partition BY discussions.created_at, discussions.title, users.username ORDER BY threads.id DESC) AS rn FROM discussions LEFT JOIN threads ON discussions.id=threads.discussion_id LEFT JOIN discussion_views ON discussions.id=discussion_views.discussion_id JOIN users ON users.id=discussions.user_id JOIN users latest_poster ON (latest_poster.id=threads.user_id) WHERE discussions.course_id={$course_id} ) sq WHERE rn=1 ORDER BY created_at DESC" |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | SELECT * FROM ( SELECT rownum AS row_id, <your other selects> FROM <your query> ORDER BY <whatever> ) DATA WHERE DATA.row_id >= 123 AND DATA.row_id <= 222 |