dagda1

replies

May 28th, 2014
333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --DROP TABLE emails;
  2.  
  3. CREATE TABLE emails
  4. (
  5.   id serial NOT NULL,
  6.   subject text,
  7.   created_at timestamp without time zone,
  8.   updated_at timestamp without time zone,
  9.   folder text,
  10.   message_id text,
  11.   CONSTRAINT emails_pkey PRIMARY KEY (id)
  12. )
  13. WITH (
  14.   OIDS=FALSE
  15. );
  16.  
  17. -- Index: emails_message_id_index
  18.  
  19. -- DROP INDEX emails_message_id_index;
  20.  
  21. CREATE INDEX emails_message_id_index
  22.   ON emails
  23.   USING btree
  24.   (message_id COLLATE pg_catalog."default");
  25.  
  26.  INSERT INTO emails(
  27.     subject, created_at, updated_at, folder, message_id)
  28.     VALUES ('howzitgoin', now(), now(), null, '53856b1448c89_23fa9605badd015951@3a139e8c-0b81-42c2-8e59-133c262e96a9.mail');
  29.  
  30. INSERT INTO emails(
  31.     subject, created_at, updated_at, folder, message_id)
  32.     VALUES ('Re: howzitgoin', now(), now(), 'INBOX', 'CAEBV8YTu_A6LtP_uGuQ-QSVj3zojWUiwcjGZpsPPEz1Pj3_i1A@mail.gmail.com');
  33.  
  34.  INSERT INTO emails(
  35.     subject, created_at, updated_at, folder, message_id)
  36.     VALUES ('Re: howzitgoin', now(), now(), 'INBOX', '53856b88a2a09_23fa9605badd01601b@3a139e8c-0b81-42c2-8e59-133c262e96a9.mail');
  37.  
  38. INSERT INTO emails(
  39.     subject, created_at, updated_at, folder, message_id)
  40.     VALUES ('Re: howzitgoin', now(), now(), 'INBOX', 'CAEBV8YT6vx6buOKUga4f=bcNGq_=WzwiqEzm2FWm3HoLZ8SbJA@mail.gmail.com');
  41.  
  42. INSERT INTO emails(
  43.     subject, created_at, updated_at, folder,  message_id)
  44.     VALUES ('Re: howzitgoin', now(), now(), 'INBOX', 'CAEBV8YRGwkNSb3cxquS5abSCmnwLn37GxCpg74mQe7=3SC5cdQ@mail.gmail.com');
  45.  
  46. INSERT INTO emails(
  47.     subject, created_at, updated_at, folder,  message_id)
  48.     VALUES ('Not part of thread', now(), now(), 'INBOX', 'CAEBV8YRGwkNSb3cxquS5abSCmnwLn37GxCpg74mQe7=3SC5cdQ@mail.gmail.com');
  49.    
  50. -- Table: replies
  51.  
  52. -- DROP TABLE replies;
  53.  
  54. CREATE TABLE replies
  55. (
  56.   id serial NOT NULL,
  57.   message_id text,
  58.   created_at timestamp without time zone,
  59.   email_id integer NOT NULL,
  60.   CONSTRAINT replies_pkey PRIMARY KEY (id),
  61.   CONSTRAINT replies_email_id_fkey FOREIGN KEY (email_id)
  62.       REFERENCES emails (id) MATCH SIMPLE
  63.       ON UPDATE NO ACTION ON DELETE CASCADE
  64. )
  65. WITH (
  66.   OIDS=FALSE
  67. );
  68.  
  69.  
  70. -- Index: replies_email_id_index
  71.  
  72. -- DROP INDEX replies_email_id_index;
  73.  
  74. CREATE INDEX replies_email_id_index
  75.   ON replies
  76.   USING btree
  77.   (email_id);
  78.  
  79. -- Index: replies_message_id_index
  80.  
  81. -- DROP INDEX replies_message_id_index;
  82.  
  83. CREATE INDEX replies_message_id_index
  84.   ON replies
  85.   USING btree
  86.   (message_id COLLATE pg_catalog."default");
  87.  
  88.  
  89.  
  90. INSERT INTO replies(message_id, created_at, email_id)
  91.             VALUES ('53856b1448c89_23fa9605badd015951@3a139e8c-0b81-42c2-8e59-133c262e96a9.mail', now(), 2);
  92.  
  93. INSERT INTO replies(message_id, created_at, email_id)
  94.             VALUES ('53856b1448c89_23fa9605badd015951@3a139e8c-0b81-42c2-8e59-133c262e96a9.mail', now(), 3);
  95.  
  96. INSERT INTO replies(message_id, created_at, email_id)
  97.             VALUES ('CAEBV8YTu_A6LtP_uGuQ-QSVj3zojWUiwcjGZpsPPEz1Pj3_i1A@mail.gmail.com', now(), 3);
  98.  
  99. INSERT INTO replies(message_id, created_at, email_id)
  100.             VALUES ('53856b88a2a09_23fa9605badd01601b@3a139e8c-0b81-42c2-8e59-133c262e96a9.mail', now(), 4);
  101.  
  102. INSERT INTO replies(message_id, created_at, email_id)
  103.             VALUES ('CAEBV8YTu_A6LtP_uGuQ-QSVj3zojWUiwcjGZpsPPEz1Pj3_i1A@mail.gmail.com', now(), 4);
  104.  
  105. INSERT INTO replies(message_id, created_at, email_id)
  106.             VALUES ('53856b1448c89_23fa9605badd015951@3a139e8c-0b81-42c2-8e59-133c262e96a9.mail', now(), 4);
  107.  
  108. INSERT INTO replies(message_id, created_at, email_id)
  109.             VALUES ('53856b88a2a09_23fa9605badd01601b@3a139e8c-0b81-42c2-8e59-133c262e96a9.mail', now(), 5);
  110.  
  111. INSERT INTO replies(message_id, created_at, email_id)
  112.             VALUES ('CAEBV8YTu_A6LtP_uGuQ-QSVj3zojWUiwcjGZpsPPEz1Pj3_i1A@mail.gmail.com', now(), 5);
  113.  
  114. INSERT INTO replies(message_id, created_at, email_id)
  115.             VALUES ('53856b1448c89_23fa9605badd015951@3a139e8c-0b81-42c2-8e59-133c262e96a9.mail', now(), 5);
  116.  
  117.  
  118. SELECT  "emails".id, "emails".message_id, subject, "count", "emails".created_at, "count"
  119. FROM "emails"  
  120. LEFT JOIN
  121. (
  122.   SELECT "email_id", count(*) AS "count" FROM "replies" GROUP BY "email_id"
  123. ) AS "r"
  124. ON ("emails"."id" = "r"."email_id")
  125. WHERE (("folder" = 'INBOX')
  126.   --AND ("emails"."id" NOT IN (SELECT "email_id" FROM "replies"))
  127. ) ORDER BY "created_at" DESC LIMIT 7 OFFSET 0
Advertisement
Add Comment
Please, Sign In to add comment