q10.sql 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. \set personId 6597069770479
  2. \set country '\'Italy\''
  3. \set tagClass '\'Thing\''
  4. \set minPathDistance 3 -- fixed value
  5. \set maxPathDistance 4 -- fixed value
  6. -- materialize=> \i q10.sql
  7. -- person.id | tag.name | messagecount
  8. -- ----------------+-----------------------------------+--------------
  9. -- 2199023257601 | Ralph_Vaughan_Williams | 3
  10. -- 2199023257601 | Aristophanes | 2
  11. -- 15393162799165 | Aristophanes | 2
  12. -- 19791209309107 | Aristophanes | 2
  13. -- 6079 | Augustine_of_Hippo | 2
  14. -- 30786325585300 | Bob_Hope | 2
  15. -- 2199023257601 | Canada | 2
  16. -- 15393162799165 | Dmitry_Medvedev | 2
  17. -- 2199023259874 | Franklin_D._Roosevelt | 2
  18. -- 15393162793184 | Interpol | 2
  19. -- 2199023257601 | Jean-Baptiste_Lamarck | 2
  20. -- 2199023261968 | Johannes_Brahms | 2
  21. -- 19791209309107 | John_Cage | 2
  22. -- 19791209309107 | Leonard_Bernstein | 2
  23. -- 2199023259874 | Ludwig_van_Beethoven | 2
  24. -- 4398046512280 | Muammar_Gaddafi | 2
  25. -- 2199023257601 | Mughal_Empire | 2
  26. -- 30786325581417 | Plácido_Domingo | 2
  27. -- 2199023257601 | Slavoj_Žižek | 2
  28. -- 30786325581417 | Wayne_Gretzky | 2
  29. -- 2199023261968 | Wolfgang_Amadeus_Mozart | 2
  30. -- 9043 | 14th_Dalai_Lama | 1
  31. -- 2199023257601 | 14th_Dalai_Lama | 1
  32. -- 4398046512280 | 14th_Dalai_Lama | 1
  33. -- 217 | 99_Luftballons | 1
  34. -- 30786325585300 | A_Day_Without_Rain | 1
  35. -- 28587302322352 | A_Kind_of_Magic | 1
  36. -- 2199023259874 | A_Little_Bit_Me,_a_Little_Bit_You | 1
  37. -- 7684 | A_Whiter_Shade_of_Pale | 1
  38. -- 15393162793184 | Aaron_Copland | 1
  39. -- 17592186053088 | Abbasid_Caliphate | 1
  40. -- 32985348842018 | Adolf_Hitler | 1
  41. -- 217 | Alan_Moore | 1
  42. -- 19791209309107 | Albert_Einstein | 1
  43. -- 217 | Alexander_Downer | 1
  44. -- 19791209310154 | Alexander_Pushkin | 1
  45. -- 4398046517288 | Alfred,_Lord_Tennyson | 1
  46. -- 2199023259874 | Alfred_Hitchcock | 1
  47. -- 9043 | Ali | 1
  48. -- 2199023257601 | Ali | 1
  49. -- 4398046512280 | Ali | 1
  50. -- 21990232560354 | Ali | 1
  51. -- 21990232564061 | All_the_Roadrunning | 1
  52. -- 32985348842085 | Ammon | 1
  53. -- 6079 | Andrew_Jackson | 1
  54. -- 19791209309107 | Angola | 1
  55. -- 30786325581056 | Another_One_Bites_the_Dust | 1
  56. -- 2199023257601 | Anton_Chekhov | 1
  57. -- 4398046512280 | Are_You_Experienced | 1
  58. -- 21990232564061 | Arenberg | 1
  59. -- 217 | Aristophanes | 1
  60. -- 2199023259874 | Aristophanes | 1
  61. -- 30786325585300 | Aristophanes | 1
  62. -- 6079 | Aristotle | 1
  63. -- 10995116279128 | Arnold_Schwarzenegger | 1
  64. -- 32985348842085 | Arthur_William_Baden_Powell | 1
  65. -- 19791209309107 | Ashikaga_shogunate | 1
  66. -- 30786325585300 | At_Last | 1
  67. -- 28587302322352 | At_War_with_the_Mystics | 1
  68. -- 30786325581417 | Augustin_Pyramus_de_Candolle | 1
  69. -- 10995116285979 | Augustine_of_Hippo | 1
  70. -- 32985348842085 | Augustine_of_Hippo | 1
  71. -- 9043 | Austrian_Empire | 1
  72. -- 19791209310154 | Banned_in_the_U.S.A. | 1
  73. -- 2199023257601 | Barbra_Streisand | 1
  74. -- 2199023257601 | Barrio_Fino | 1
  75. -- 2199023259874 | Barry_Manilow | 1
  76. -- 2199023259874 | Before_You_Were_Punk | 1
  77. -- 30786325581417 | Benjamin_Harrison | 1
  78. -- 30786325585300 | Bertolt_Brecht | 1
  79. -- 19791209309107 | Bill_Clinton | 1
  80. -- 30786325585300 | Bill_Clinton | 1
  81. -- 32985348842018 | Blood_Sugar_Sex_Magik | 1
  82. -- 2199023257601 | Bob_Dole | 1
  83. -- 19791209309107 | Bob_Dylan | 1
  84. -- 32985348842085 | Botswana | 1
  85. -- 10995116279128 | Break_Like_the_Wind | 1
  86. -- 30786325585300 | Breathe_Again | 1
  87. -- 21990232564061 | British_Ceylon | 1
  88. -- 2199023261968 | British_Empire | 1
  89. -- 2199023257601 | Burma | 1
  90. -- 19791209309107 | Busta_Rhymes | 1
  91. -- 30786325581417 | Busta_Rhymes | 1
  92. -- 30786325585300 | Béla_Bartók | 1
  93. -- 19791209309107 | C._S._Lewis | 1
  94. -- 21990232564061 | Cameroon | 1
  95. -- 35184372093958 | Cannot_Buy_My_Soul | 1
  96. -- 2199023257601 | Cape_Verde | 1
  97. -- 19791209310154 | Carl_Gustaf_Emil_Mannerheim | 1
  98. -- 4398046518779 | Chaka_Khan | 1
  99. -- 2199023259874 | Champa | 1
  100. -- 7684 | Charles_Dickens | 1
  101. -- 32985348842085 | Charles_II_of_England | 1
  102. -- 30786325585300 | Charles_I_of_England | 1
  103. -- 15393162793184 | Charlton_Heston | 1
  104. -- 4398046517288 | Che_Guevara | 1
  105. -- 30786325581417 | Christopher_Columbus | 1
  106. -- 2199023257601 | Chrome_Dreams_II | 1
  107. -- 10995116279128 | Claude_Debussy | 1
  108. -- 10995116285979 | Claude_Debussy | 1
  109. -- (100 rows)
  110. --
  111. -- Time: 22849.938 ms (00:22.850)
  112. /* Q10. Experts in social circle
  113. \set personId 30786325588624
  114. \set country '\'China\''
  115. \set tagClass '\'MusicalArtist\''
  116. \set minPathDistance 3 -- fixed value
  117. \set maxPathDistance 4 -- fixed value
  118. */
  119. WITH friends AS
  120. (SELECT Person2Id
  121. FROM Person_knows_Person
  122. WHERE Person1Id = :personId
  123. )
  124. , friends_of_friends AS
  125. (SELECT knowsB.Person2Id AS Person2Id
  126. FROM friends
  127. JOIN Person_knows_Person knowsB
  128. ON friends.Person2Id = knowsB.Person1Id
  129. )
  130. , friends_and_friends_of_friends AS
  131. (SELECT Person2Id
  132. FROM friends
  133. UNION -- using plain UNION to eliminate duplicates
  134. SELECT Person2Id
  135. FROM friends_of_friends
  136. )
  137. , friends_between_3_and_4_hops AS (
  138. -- people reachable through 1..4 hops
  139. (SELECT DISTINCT knowsD.Person2Id AS Person2Id
  140. FROM friends_and_friends_of_friends ffoaf
  141. JOIN Person_knows_Person knowsC
  142. ON knowsC.Person1Id = ffoaf.Person2Id
  143. JOIN Person_knows_Person knowsD
  144. ON knowsD.Person1Id = knowsC.Person2Id
  145. )
  146. -- removing people reachable through 1..2 hops, yielding the ones reachable through 3..4 hops
  147. EXCEPT
  148. (SELECT Person2Id
  149. FROM friends_and_friends_of_friends
  150. )
  151. )
  152. , friend_list AS (
  153. SELECT f.person2Id AS friendId
  154. FROM friends_between_3_and_4_hops f
  155. JOIN Person tf -- the friend's person record
  156. ON tf.id = f.person2Id
  157. JOIN City
  158. ON City.id = tf.LocationCityId
  159. JOIN Country
  160. ON Country.id = City.PartOfCountryId
  161. AND Country.name = :country
  162. )
  163. , messages_of_tagclass_by_friends AS (
  164. SELECT DISTINCT f.friendId
  165. , Message.MessageId AS messageid
  166. FROM friend_list f
  167. JOIN Message
  168. ON Message.CreatorPersonId = f.friendId
  169. JOIN Message_hasTag_Tag
  170. ON Message_hasTag_Tag.MessageId = Message.MessageId
  171. JOIN Tag
  172. ON Tag.id = Message_hasTag_Tag.TagId
  173. JOIN TagClass
  174. ON TagClass.id = Tag.TypeTagClassId
  175. WHERE TagClass.name = :tagClass
  176. )
  177. SELECT m.friendId AS "person.id"
  178. , Tag.name AS "tag.name"
  179. , count(*) AS messageCount
  180. FROM messages_of_tagclass_by_friends m
  181. JOIN Message_hasTag_Tag
  182. ON Message_hasTag_Tag.MessageId = m.MessageId
  183. JOIN Tag
  184. ON Tag.id = Message_hasTag_Tag.TagId
  185. GROUP BY m.friendId, Tag.name
  186. ORDER BY messageCount DESC, Tag.name, m.friendId
  187. LIMIT 100
  188. ;