Advertisement
arxeiss

SQL Count all rows

Dec 7th, 2014
468
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 0.78 KB | None | 0 0
  1. -- Found on StackOverflow: http://stackoverflow.com/questions/21537511/sql-server-query-with-pagination-and-count
  2. -- WHERE must be in WITH statemens
  3.  
  4. WITH users_out AS (SELECT id, login, root FROM users WHERE login LIKE 'kut%' )
  5.  
  6. SELECT id, login, root, cntUsers.cnt AS usersCount, COUNT(*) OVER() AS badUsersCount
  7. FROM users_out
  8.     CROSS JOIN (SELECT COUNT(*) AS cnt FROM users_out) AS cntUsers
  9. ORDER BY login
  10. OFFSET 0 ROWS
  11. FETCH NEXT 10 ROWS ONLY;
  12.  
  13. -- Old bad version
  14. WITH users_out AS (SELECT id, login, root FROM users)
  15.  
  16. SELECT id, login, root, cntUsers.cnt AS usersCount, COUNT(*) OVER() AS badUsersCount
  17. FROM users_out
  18.     CROSS JOIN (SELECT COUNT(*) AS cnt FROM users_out) AS cntUsers
  19. WHERE users_out.login LIKE 'kut%'
  20. ORDER BY login
  21. OFFSET 0 ROWS
  22. FETCH NEXT 10 ROWS ONLY;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement