Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
386
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. +------+------+
  2. | id | res |
  3. +------+------+
  4. | 1 | 55 |
  5. | 2 | 10 |
  6. | 3 | 89 |
  7. | 4 | 100 |
  8. | 5 | 80 |
  9. | 6 | 55 |
  10. | 7 | 70 |
  11. | 8 | 35 |
  12. | 9 | 46 |
  13. | 10 | 51 |
  14. +------+------+
  15.  
  16. +------+------+
  17. | id |count |
  18. +------+------+
  19. | 1 | 4 |
  20. | 2 | 0 |
  21. | 3 | 8 |
  22. | 4 | 9 |
  23. | 5 | 7 |
  24. | 6 | 4 |
  25. | 7 | 6 |
  26. | 8 | 1 |
  27. | 9 | 2 |
  28. | 10 | 3 |
  29. +------+------+
  30.  
  31. select
  32. id,
  33. res,
  34. rank() over (ORDER BY res) as rank
  35. from
  36. my_table
  37. order by
  38. res
  39.  
  40. +-----+------+
  41. | id | _c1 |
  42. +-----+------+
  43. | 1 | 4 |
  44. | 2 | 0 |
  45. | 3 | 8 |
  46. | 4 | 9 |
  47. | 5 | 7 |
  48. | 6 | 4 |
  49. | 7 | 6 |
  50. | 8 | 1 |
  51. | 9 | 2 |
  52. | 10 | 3 |
  53. +-----+------+
  54.  
  55. SELECT id, SUM(IF ( c.res1 > c.res2, 1 , 0 ))
  56. FROM (
  57. SELECT id, a.res AS res1, b.res AS res2
  58. FROM test_4 AS a
  59. INNER JOIN (
  60. SELECT res
  61. FROM test_4
  62. ) b
  63. ) c
  64. GROUP BY id;
  65.  
  66. select
  67. id,
  68. res,
  69. rank() over (ORDER BY res) -1 as rank
  70. FROM point
  71.  
  72. ORDER BY id
  73.  
  74. Select id, sum(comparison) as count
  75. From (
  76.  
  77. Select
  78. a.id,
  79. a.res as res1,
  80. b.res as res2,
  81. Case when a.res > b.res then 1
  82. Else 0
  83. End as comparison
  84.  
  85. FROM point a
  86. CROSS JOIN point b
  87. ) c
  88.  
  89. GROUP BY id
  90.  
  91. SELECT mt.id AS id
  92. , mt.res AS res
  93. , COUNT(1) OVER (PARTITION BY NULL ORDER BY mt.res ASC ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) - 1 AS cnt
  94. FROM my_table mt
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement