q02.sql 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. \set date '\'2010-06-08\'::timestamp'
  2. \set tagClass '\'ChristianBishop\''
  3. -- materialize=> \i q02.sql
  4. -- tag.name | coalesce | coalesce | diff
  5. -- -------------------------+----------+----------+------
  6. -- Pope_John_XXIII | 13 | 36 | 23
  7. -- Pope_Pius_IX | 19 | 38 | 19
  8. -- Pope_John_Paul_II | 38 | 54 | 16
  9. -- Pope_Benedict_XVI | 27 | 40 | 13
  10. -- Pope_Pius_X | 22 | 25 | 3
  11. -- Pope_Leo_XIII | 24 | 26 | 2
  12. -- Pope_Paul_VI | 37 | 35 | 2
  13. -- Pope_Pius_XI | 20 | 22 | 2
  14. -- Pope_Gregory_I | 15 | 16 | 1
  15. -- Pope_Gregory_IX | 1 | 0 | 1
  16. -- Pope_Pius_XII | 32 | 33 | 1
  17. -- Ambrose | 1 | 1 | 0
  18. -- Anselm_of_Canterbury | 0 | 0 | 0
  19. -- Augustine_of_Canterbury | 0 | 0 | 0
  20. -- Charles_Borromeo | 0 | 0 | 0
  21. -- John_Henry_Newman | 0 | 0 | 0
  22. -- Pope_Adrian_IV | 0 | 0 | 0
  23. -- Pope_Alexander_III | 0 | 0 | 0
  24. -- Pope_Alexander_VI | 0 | 0 | 0
  25. -- Pope_Alexander_VII | 0 | 0 | 0
  26. -- Pope_Benedict_XIV | 0 | 0 | 0
  27. -- Pope_Boniface_VIII | 0 | 0 | 0
  28. -- Pope_Clement_V | 0 | 0 | 0
  29. -- Pope_Clement_VII | 0 | 0 | 0
  30. -- Pope_Clement_VIII | 0 | 0 | 0
  31. -- Pope_Clement_XI | 0 | 0 | 0
  32. -- Pope_Eugene_III | 0 | 0 | 0
  33. -- Pope_Eugene_IV | 0 | 0 | 0
  34. -- Pope_Gregory_XIII | 0 | 0 | 0
  35. -- Pope_Gregory_XVI | 0 | 0 | 0
  36. -- Pope_Innocent_III | 0 | 0 | 0
  37. -- Pope_Innocent_X | 0 | 0 | 0
  38. -- Pope_Innocent_XI | 0 | 0 | 0
  39. -- Pope_John_Paul_I | 0 | 0 | 0
  40. -- Pope_Julius_II | 0 | 0 | 0
  41. -- Pope_Leo_I | 0 | 0 | 0
  42. -- Pope_Leo_X | 0 | 0 | 0
  43. -- Pope_Paul_III | 0 | 0 | 0
  44. -- Pope_Paul_IV | 0 | 0 | 0
  45. -- Pope_Paul_V | 0 | 0 | 0
  46. -- Pope_Pius_II | 0 | 0 | 0
  47. -- Pope_Pius_IV | 0 | 0 | 0
  48. -- Pope_Pius_VI | 0 | 0 | 0
  49. -- Pope_Pius_VII | 0 | 0 | 0
  50. -- Pope_Sixtus_IV | 0 | 0 | 0
  51. -- Pope_Sixtus_V | 0 | 0 | 0
  52. -- Pope_Urban_VIII | 0 | 0 | 0
  53. -- Rowan_Williams | 0 | 0 | 0
  54. -- Thomas_Becket | 0 | 0 | 0
  55. -- (49 rows)
  56. --
  57. -- Time: 42939.038 ms (00:42.939)
  58. /* Q2. Tag evolution
  59. \set date '\'2012-06-01\''::timestamp
  60. \set tagClass '\'MusicalArtist\''
  61. */
  62. WITH
  63. MyTag AS (
  64. SELECT Tag.id AS id, Tag.name AS name
  65. FROM TagClass
  66. JOIN Tag
  67. ON Tag.TypeTagClassId = TagClass.id
  68. WHERE TagClass.name = :tagClass
  69. ),
  70. detail AS (
  71. SELECT t.id as TagId
  72. , count(CASE WHEN Message.creationDate < :date + INTERVAL '100 days' THEN Message.MessageId ELSE NULL END) AS countMonth1
  73. , count(CASE WHEN Message.creationDate >= :date + INTERVAL '100 days' THEN Message.MessageId ELSE NULL END) AS countMonth2
  74. FROM MyTag t
  75. JOIN Message_hasTag_Tag
  76. ON Message_hasTag_tag.TagId = t.id
  77. JOIN Message
  78. ON Message.MessageId = Message_hasTag_tag.MessageId
  79. AND Message.creationDate >= :date
  80. AND Message.creationDate < :date + INTERVAL '200 days'
  81. GROUP BY t.id
  82. )
  83. SELECT t.name AS "tag.name"
  84. , coalesce(countMonth1, 0)
  85. , coalesce(countMonth2, 0)
  86. , abs(coalesce(countMonth1, 0)-coalesce(countMonth2, 0)) AS diff
  87. FROM MyTag t LEFT JOIN detail ON t.id = detail.TagId
  88. ORDER BY diff desc, t.name
  89. LIMIT 100
  90. ;