q12.sql 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. -- TODO(mgree) we're using a different parameter format (raw table for IN expression) instead of an array
  2. \set startDate '\'2012-06-03\''::timestamp
  3. \set lengthThreshold '120'
  4. \set languages '(\'es\', \'ta\', \'pt\')'
  5. -- materialize=> \i q12.sql
  6. -- messagecount | personcount
  7. -- --------------+-------------
  8. -- 0 | 3833
  9. -- 1 | 622
  10. -- 2 | 583
  11. -- 3 | 446
  12. -- 4 | 425
  13. -- 5 | 365
  14. -- 6 | 332
  15. -- 7 | 272
  16. -- 8 | 246
  17. -- 9 | 237
  18. -- 11 | 190
  19. -- 10 | 187
  20. -- 12 | 167
  21. -- 13 | 166
  22. -- 14 | 157
  23. -- 16 | 135
  24. -- 15 | 119
  25. -- 18 | 105
  26. -- 19 | 96
  27. -- 17 | 92
  28. -- 20 | 90
  29. -- 22 | 80
  30. -- 21 | 78
  31. -- 23 | 76
  32. -- 24 | 63
  33. -- 25 | 58
  34. -- 28 | 57
  35. -- 26 | 47
  36. -- 29 | 45
  37. -- 30 | 43
  38. -- 32 | 41
  39. -- 31 | 41
  40. -- 27 | 41
  41. -- 35 | 37
  42. -- 34 | 37
  43. -- 33 | 35
  44. -- 39 | 31
  45. -- 43 | 23
  46. -- 38 | 23
  47. -- 37 | 23
  48. -- 36 | 23
  49. -- 42 | 22
  50. -- 55 | 17
  51. -- 41 | 17
  52. -- 49 | 16
  53. -- 45 | 16
  54. -- 40 | 16
  55. -- 52 | 15
  56. -- 46 | 15
  57. -- 54 | 14
  58. -- 47 | 14
  59. -- 57 | 13
  60. -- 61 | 12
  61. -- 44 | 12
  62. -- 70 | 11
  63. -- 53 | 10
  64. -- 51 | 10
  65. -- 62 | 9
  66. -- 50 | 9
  67. -- 48 | 9
  68. -- 77 | 8
  69. -- 64 | 8
  70. -- 56 | 8
  71. -- 87 | 7
  72. -- 75 | 7
  73. -- 71 | 7
  74. -- 68 | 7
  75. -- 67 | 7
  76. -- 63 | 7
  77. -- 60 | 7
  78. -- 58 | 7
  79. -- 73 | 6
  80. -- 59 | 6
  81. -- 96 | 5
  82. -- 89 | 5
  83. -- 76 | 5
  84. -- 119 | 4
  85. -- 95 | 4
  86. -- 79 | 4
  87. -- 74 | 4
  88. -- 138 | 3
  89. -- 137 | 3
  90. -- 125 | 3
  91. -- 124 | 3
  92. -- 106 | 3
  93. -- 103 | 3
  94. -- 101 | 3
  95. -- 97 | 3
  96. -- 94 | 3
  97. -- 93 | 3
  98. -- 92 | 3
  99. -- 90 | 3
  100. -- 85 | 3
  101. -- 82 | 3
  102. -- 81 | 3
  103. -- 78 | 3
  104. -- 72 | 3
  105. -- 69 | 3
  106. -- 66 | 3
  107. -- 65 | 3
  108. -- 249 | 2
  109. -- 173 | 2
  110. -- 154 | 2
  111. -- 146 | 2
  112. -- 141 | 2
  113. -- 133 | 2
  114. -- 123 | 2
  115. -- 116 | 2
  116. -- 111 | 2
  117. -- 108 | 2
  118. -- 105 | 2
  119. -- 100 | 2
  120. -- 91 | 2
  121. -- 83 | 2
  122. -- 526 | 1
  123. -- 474 | 1
  124. -- 416 | 1
  125. -- 406 | 1
  126. -- 385 | 1
  127. -- 364 | 1
  128. -- 335 | 1
  129. -- 310 | 1
  130. -- 277 | 1
  131. -- 262 | 1
  132. -- 247 | 1
  133. -- 245 | 1
  134. -- 218 | 1
  135. -- 217 | 1
  136. -- 216 | 1
  137. -- 215 | 1
  138. -- 214 | 1
  139. -- 206 | 1
  140. -- 205 | 1
  141. -- 197 | 1
  142. -- 181 | 1
  143. -- 179 | 1
  144. -- 171 | 1
  145. -- 170 | 1
  146. -- 166 | 1
  147. -- 164 | 1
  148. -- 161 | 1
  149. -- 160 | 1
  150. -- 157 | 1
  151. -- 156 | 1
  152. -- 155 | 1
  153. -- 152 | 1
  154. -- 151 | 1
  155. -- 150 | 1
  156. -- 147 | 1
  157. -- 144 | 1
  158. -- 143 | 1
  159. -- 139 | 1
  160. -- 136 | 1
  161. -- 134 | 1
  162. -- 131 | 1
  163. -- 130 | 1
  164. -- 129 | 1
  165. -- 128 | 1
  166. -- 126 | 1
  167. -- 122 | 1
  168. -- 121 | 1
  169. -- 118 | 1
  170. -- 117 | 1
  171. -- 115 | 1
  172. -- 113 | 1
  173. -- 112 | 1
  174. -- 110 | 1
  175. -- 107 | 1
  176. -- 102 | 1
  177. -- 99 | 1
  178. -- 88 | 1
  179. -- 86 | 1
  180. -- 84 | 1
  181. -- 80 | 1
  182. -- (174 rows)
  183. --
  184. -- Time: 5061.933 ms (00:05.062)
  185. /* Q12. How many persons have a given number of messages
  186. \set startDate '\'2010-07-22\''::timestamp
  187. \set lengthThreshold '20'
  188. \set languages '\'{"ar", "hu"}\''::varchar[]
  189. */
  190. WITH
  191. matching_message AS (
  192. SELECT MessageId,
  193. CreatorPersonId
  194. FROM Message
  195. WHERE Message.content IS NOT NULL
  196. AND Message.length < :lengthThreshold
  197. AND Message.creationDate > :startDate
  198. AND Message.RootPostLanguage IN :languages -- MZ change to use postgres containment check
  199. ),
  200. person_w_posts AS (
  201. SELECT Person.id, count(matching_message.MessageId) as messageCount
  202. FROM Person
  203. LEFT JOIN matching_message
  204. ON Person.id = matching_message.CreatorPersonId
  205. GROUP BY Person.id
  206. ),
  207. message_count_distribution AS (
  208. SELECT pp.messageCount, count(*) as personCount
  209. FROM person_w_posts pp
  210. GROUP BY pp.messageCount
  211. ORDER BY personCount DESC, messageCount DESC
  212. )
  213. SELECT *
  214. FROM message_count_distribution
  215. ORDER BY personCount DESC, messageCount DESC
  216. ;
  217. -- materialize=> \i q12.sql
  218. -- ^CCancel request sent
  219. -- psql:q12.sql:30: ERROR: canceling statement due to user request
  220. -- Time: 1030151.977 ms (17:10.152)
  221. -- WITH person_w_posts AS (
  222. -- SELECT Person.id, count(Message.MessageId) as messageCount
  223. -- FROM Person
  224. -- LEFT JOIN Message
  225. -- ON Person.id = Message.CreatorPersonId
  226. -- AND Message.content IS NOT NULL
  227. -- AND Message.length < :lengthThreshold
  228. -- AND Message.creationDate > :startDate
  229. -- AND Message.RootPostLanguage = ANY (:languages) -- MZ change to use postgres containment check
  230. -- GROUP BY Person.id
  231. -- )
  232. -- , message_count_distribution AS (
  233. -- SELECT pp.messageCount, count(*) as personCount
  234. -- FROM person_w_posts pp
  235. -- GROUP BY pp.messageCount
  236. -- ORDER BY personCount DESC, messageCount DESC
  237. -- )
  238. -- SELECT *
  239. -- FROM message_count_distribution
  240. -- ORDER BY personCount DESC, messageCount DESC
  241. -- ;