code_junkie

Query to find nth max value of a column

Nov 10th, 2011
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. SELECT DOB FROM (SELECT DOB FROM USERS ORDER BY DOB DESC) WHERE ROWID = 6
  2.  
  3. SELECT column FROM table ORDER BY column DESC LIMIT 7,10;
  4.  
  5. select id from table order by id desc limit 4 ;
  6. +------+
  7. | id |
  8. +------+
  9. | 2211 |
  10. | 2210 |
  11. | 2209 |
  12. | 2208 |
  13. +------+
  14.  
  15.  
  16. SELECT yourvalue
  17. FROM yourtable t1
  18. WHERE EXISTS( SELECT COUNT(*)
  19. FROM yourtable t2
  20. WHERE t1.id <> t2.id
  21. AND t1.yourvalue < t2.yourvalue
  22. HAVING COUNT(*) = 3 )
  23.  
  24.  
  25. +------+
  26. | id |
  27. +------+
  28. | 2208 |
  29. +------+
  30.  
  31. select created from (
  32. select created from (
  33. select created from user_objects
  34. order by created desc
  35. )
  36. where rownum <= 9
  37. order by created asc
  38. )
  39. where rownum = 1
  40.  
  41. select SAL from EMPLOYEE E1 where
  42. (N - 1) = (select count(distinct(SAL))
  43. from EMPLOYEE E2
  44. where E2.SAL > E1.SAL )
  45.  
  46. select SAL from EMPLOYEE E1 where
  47. (N - 1) = (select count(distinct(SAL))
  48. from EMPLOYEE E2
  49. where E2.SAL > E1.SAL )
  50.  
  51. select SAL from EMPLOYEE E1 where
  52. (2 - 1) = (select count(distinct(SAL))
  53. from EMPLOYEE E2
  54. where E2.SAL > E1.SAL )
  55.  
  56. select distinct top n+1 column from table order by column desc
  57.  
  58. select distinct top n+1 column from table order by column desc
  59.  
  60. SELECT col1 from
  61. (select col1, dense_rank(col1) over (order by col1 desc) ranking
  62. from t1) subq where ranking between 2 and @n
  63.  
  64. select col1
  65. from
  66. ( select col1, dense_rank() over (order by col1 desc) rnk
  67. from tbl
  68. )
  69. where rnk = :b1
  70.  
  71. SELECT MIN(q.col1) FROM (
  72. SELECT
  73. DISTINCT TOP n col1
  74. FROM myTable
  75. ORDER BY col1 DESC
  76. ) q;
Add Comment
Please, Sign In to add comment