Advertisement
Guest User

Untitled

a guest
Apr 27th, 2015
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. CREATE VIEW access AS
  2. SELECT c.id AS content_id, u.id AS user_id,
  3. LEAST(MIN(simple.relevant_date),
  4. MIN(with_prereq.relevant_date),
  5. MIN(with_delay.relevant_date),
  6. MIN(with_both.relevant_date)) AS relevant_date
  7. FROM content AS c
  8. LEFT OUTER JOIN simple ON simple.content_id = c.id
  9. LEFT OUTER JOIN with_prereq ON with_prereq.content_id = c.id
  10. LEFT OUTER JOIN with_delay ON with_delay.content_id = c.id
  11. LEFT OUTER JOIN with_both ON with_both.content_id = c.id
  12. CROSS JOIN base_user AS u
  13. WHERE u.id = COALESCE(
  14. simple.user_id, with_delay.user_id, with_prereq.user_id,
  15. with_both.user_id)
  16. GROUP BY c.id, u.id;
  17.  
  18. CREATE VIEW simple AS
  19. SELECT cc.content_id AS content_id,
  20. cu.user_id AS user_id,
  21. cc.relevant_date AS relevant_date
  22. FROM circle AS c
  23. JOIN circlecontent AS cc ON cc.circle_id = c.id
  24. JOIN circleuser AS cu ON cu.circle_id = c.id
  25. WHERE (cc.delay IS NULL OR cc.delay = 0)
  26. AND cc.prereq_id IS NULL;
  27.  
  28. CREATE VIEW with_delay AS
  29. SELECT cc.content_id AS content_id,
  30. cu.user_id AS user_id,
  31. cu.relevant_date AS relevant_date
  32. FROM circle AS c
  33. JOIN circlecontent AS cc ON cc.circle_id = c.id
  34. JOIN circleuser AS cu ON cu.circle_id = c.id
  35. WHERE (cc.delay IS NOT NULL AND cc.delay > 0)
  36. AND cc.prereq_id IS NULL;
  37.  
  38. CREATE VIEW with_prereq AS
  39. SELECT cc.content_id AS content_id,
  40. cu.user_id AS user_id,
  41. GREATEST(cu.relevant_date, cc.relevant_date) AS relevant_date
  42. FROM circle AS c
  43. JOIN circlecontent AS cc ON cc.circle_id = c.id
  44. JOIN circleuser AS cu ON cu.circle_id = c.id
  45. WHERE (cc.delay IS NULL OR cc.delay = 0)
  46. AND cc.prereq_id IS NOT NULL;
  47.  
  48. CREATE VIEW with_both AS
  49. SELECT cc.content_id AS content_id,
  50. cu.user_id AS user_id,
  51. LEAST(cu.relevant_date, cc.relevant_date) AS relevant_date
  52. FROM circle AS c
  53. JOIN circlecontent AS cc ON cc.circle_id = c.id
  54. JOIN circleuser AS cu ON cu.circle_id = c.id
  55. WHERE (cc.delay IS NOT NULL AND cc.delay > 0)
  56. AND cc.prereq_id IS NOT NULL;
  57.  
  58. CREATE TABLE content (
  59. id SERIAL
  60. );
  61. CREATE TABLE base_user (
  62. id SERIAL
  63. );
  64. CREATE TABLE circle (
  65. id SERIAL
  66. );
  67. CREATE TABLE circleuser (
  68. circle_id INTEGER NOT NULL,
  69. user_id INTEGER NOT NULL,
  70. relevant_date TIMESTAMP with time zone
  71. );
  72. CREATE TABLE circlecontent (
  73. circle_id INTEGER NOT NULL,
  74. content_id INTEGER NOT NULL,
  75. delay INTEGER,
  76. prereq_id INTEGER,
  77. relevant_date TIMESTAMP with time zone
  78. );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement