Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Found on StackOverflow: http://stackoverflow.com/questions/21537511/sql-server-query-with-pagination-and-count
- -- WHERE must be in WITH statemens
- WITH users_out AS (SELECT id, login, root FROM users WHERE login LIKE 'kut%' )
- SELECT id, login, root, cntUsers.cnt AS usersCount, COUNT(*) OVER() AS badUsersCount
- FROM users_out
- CROSS JOIN (SELECT COUNT(*) AS cnt FROM users_out) AS cntUsers
- ORDER BY login
- OFFSET 0 ROWS
- FETCH NEXT 10 ROWS ONLY;
- -- Old bad version
- WITH users_out AS (SELECT id, login, root FROM users)
- SELECT id, login, root, cntUsers.cnt AS usersCount, COUNT(*) OVER() AS badUsersCount
- FROM users_out
- CROSS JOIN (SELECT COUNT(*) AS cnt FROM users_out) AS cntUsers
- WHERE users_out.login LIKE 'kut%'
- ORDER BY login
- OFFSET 0 ROWS
- FETCH NEXT 10 ROWS ONLY;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement