1

Possible Duplicate:
How do I limit the number of rows returned by an oracle query?

I have this query in oracle which gets all the discussions from the database with some metadata and thread discussion last poster. I want to add limit and offset to the query how can I change it in order to support it.

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"
Cœur
  • 37,241
  • 25
  • 195
  • 267
Mythriel
  • 1,360
  • 5
  • 24
  • 45

1 Answers1

1
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
MatBailie
  • 83,401
  • 18
  • 103
  • 137