On Tuesday, May 27, 2014 3:33:35 AM UTC-7, [email protected] wrote:
>
> I have the following query:
>
> SELECT "emails".id, "emails".message_id, subject, body, html, sent_at, 
> t1.message_count
> FROM "emails"
> INNER JOIN "email_participants" 
> ON ("email_participants"."email_id" = "emails"."id") 
> LEFT JOIN (
>  SELECT COUNT(*) message_count, message_id
>  FROM replies 
>  GROUP BY message_id) t1 
> ON "emails".message_id = t1.message_id 
> WHERE (("user_id" = 163) AND ("folder" = 'INBOX') 
> AND ("emails".id NOT IN (SELECT email_id FROM Replies)))
> ORDER BY "sent_at" DESC LIMIT 100 OFFSET 0;
>
>
>
> I can handle everything apart from the derived table join, I could create 
> a view but is it possible to join on  a derived table with the sequel gem?
>

Join to a dataset:

DB[:emails].inner_join(:email_participants, :email_id=>:id).
  left_join(DB[:replies].group_and_count(:message_id), 
:message_id=>:emails__message_id).
  where(:user_id=>163, :folder=>'INBOX').
  exclude(:emails__id=>DB[:replies].select(:email_id)).
  select(:emails__id, :emails__message_id, :subject, :body, :html, 
:sent_at, :t1__count).
  reverse_order(:sent_at).
  limit(100)

Thanks,
Jeremy

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.

Reply via email to