q07.sql 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. \set tag '\'Slovenia\''
  2. -- materialize=> \i q07.sql
  3. -- relatedTag.name | count
  4. -- ------------------------------------------+-------
  5. -- Humayun | 10
  6. -- Genghis_Khan | 9
  7. -- Muammar_Gaddafi | 9
  8. -- Sammy_Sosa | 8
  9. -- Thomas_Hardy | 7
  10. -- Dalai_Lama | 6
  11. -- Fernando_González | 6
  12. -- Haile_Selassie_I | 6
  13. -- Mariano_Rivera | 6
  14. -- Peter_Hain | 6
  15. -- Ronald_Reagan | 6
  16. -- Bertrand_Russell | 5
  17. -- Bill_Clinton | 5
  18. -- Bob_Dole | 5
  19. -- Diana,_Princess_of_Wales | 5
  20. -- Dimitri_Tiomkin | 5
  21. -- Duchy_of_Warsaw | 5
  22. -- Dustin_Hoffman | 5
  23. -- Gerald_Ford | 5
  24. -- Indonesia | 5
  25. -- Julius_Caesar | 5
  26. -- Monaco | 5
  27. -- Netherlands | 5
  28. -- Nicaragua | 5
  29. -- Poland | 5
  30. -- Sierra_Leone | 5
  31. -- Silvio_Berlusconi | 5
  32. -- Soviet_Union | 5
  33. -- Sudan | 5
  34. -- Tiger_Woods | 5
  35. -- Albert_Einstein | 4
  36. -- Arthur_Wellesley,_1st_Duke_of_Wellington | 4
  37. -- Australia | 4
  38. -- Axis_powers | 4
  39. -- Batavian_Republic | 4
  40. -- Bosnia_and_Herzegovina | 4
  41. -- Clark_Gable | 4
  42. -- Colin_Powell | 4
  43. -- Edward_IV_of_England | 4
  44. -- Elizabeth_II | 4
  45. -- Etruscan_civilization | 4
  46. -- Fidel_Castro | 4
  47. -- Freddie_Mercury | 4
  48. -- George_Gershwin | 4
  49. -- German_Empire | 4
  50. -- Ghana | 4
  51. -- Greece | 4
  52. -- Gustav_Mahler | 4
  53. -- Imelda_Marcos | 4
  54. -- India | 4
  55. -- James_Buchanan | 4
  56. -- Joseph_Stalin | 4
  57. -- Leo_Tolstoy | 4
  58. -- Lewis_Carroll | 4
  59. -- Louis_Philippe_I | 4
  60. -- Martin_Luther | 4
  61. -- Martin_Luther_King,_Jr. | 4
  62. -- Montenegro | 4
  63. -- Monty_Python | 4
  64. -- Moon_River | 4
  65. -- Nicolas_Sarkozy | 4
  66. -- Puerto_Rico | 4
  67. -- Raphael | 4
  68. -- Richard_Rodgers | 4
  69. -- Rwanda | 4
  70. -- Saint_Pierre_and_Miquelon | 4
  71. -- Samuel_Pepys | 4
  72. -- The_Bahamas | 4
  73. -- Tori_Amos | 4
  74. -- Turks_and_Caicos_Islands | 4
  75. -- Under_Pressure | 4
  76. -- Vatican_City | 4
  77. -- Vladimir_Lenin | 4
  78. -- Wales | 4
  79. -- William_McKinley | 4
  80. -- Abraham_Lincoln | 3
  81. -- Al_Gore | 3
  82. -- Alfred,_Lord_Tennyson | 3
  83. -- Algeria | 3
  84. -- Andy_Roddick | 3
  85. -- Aristophanes | 3
  86. -- Azad_Hind | 3
  87. -- Baby,_I_Love_Your_Way | 3
  88. -- Bangladesh | 3
  89. -- Batman | 3
  90. -- Bill_Cosby | 3
  91. -- Bing_Crosby | 3
  92. -- Bolivia | 3
  93. -- British_North_America | 3
  94. -- British_Overseas_Territories | 3
  95. -- Burma | 3
  96. -- Carl_Gustaf_Emil_Mannerheim | 3
  97. -- Cecil_B._DeMille | 3
  98. -- Charles,_Prince_of_Wales | 3
  99. -- Charles_de_Gaulle | 3
  100. -- Charlton_Heston | 3
  101. -- Christina_Aguilera | 3
  102. -- Christopher_Lee | 3
  103. -- Chuck_Berry | 3
  104. -- Chulalongkorn | 3
  105. -- (100 rows)
  106. --
  107. -- Time: 17272.877 ms (00:17.273)
  108. /* Q7. Related topics
  109. \set tag '\'Enrique_Iglesias\''
  110. */
  111. WITH MyMessage AS (
  112. SELECT m.MessageId
  113. FROM Message_hasTag_Tag m, Tag
  114. WHERE Tag.name = :tag and m.TagId = Tag.Id
  115. )
  116. SELECT RelatedTag.name AS "relatedTag.name"
  117. , count(*) AS count
  118. FROM MyMessage ParentMessage_HasTag_Tag
  119. -- as an optimization, we don't need message here as it's ID is in ParentMessage_HasTag_Tag
  120. -- so proceed to the comment directly
  121. INNER JOIN Message Comment
  122. ON ParentMessage_HasTag_Tag.MessageId = Comment.ParentMessageId
  123. -- comment's tag
  124. LEFT JOIN Message_hasTag_Tag ct
  125. ON Comment.MessageId = ct.MessageId
  126. INNER JOIN Tag RelatedTag
  127. ON RelatedTag.id = ct.TagId
  128. WHERE TRUE
  129. -- comment doesn't have the given tag
  130. AND Comment.MessageId NOT In (SELECT MessageId FROM MyMessage)
  131. AND Comment.ParentMessageId IS NOT NULL
  132. GROUP BY RelatedTag.Name
  133. ORDER BY count DESC, RelatedTag.name
  134. LIMIT 100
  135. ;