Hi Jeremy, It works. You’re legend.
Thank you Bang > On 4 May 2016, at 2:24 AM, Jeremy Evans <[email protected]> wrote: > > On Tuesday, May 3, 2016 at 8:13:00 AM UTC-7, The Bang Nguyen wrote: > Hi Jeremy, > > I did figure out the SQL query to achieve my need. > However, as a newbie to Sequel, I do not know how to turn it into a Sequel > DSL though > > Below is my SQL query: > ``` > select models.id <http://models.id/> > ,case > when (select max(events.created_at) from events where > event_type='DoSomething' and models.id <http://models.id/> = events.model_id) > is not null > and > (select max(events.created_at) from events where > event_type='UndoSomething' and models.id <http://models.id/> = > events.model_id) is null > then true > when (select max(events.created_at) from events where > event_type='DoSomething' and models.id <http://models.id/> = events.model_id) > > > (select max(events.created_at) from events where > event_type='UndoSomething' and models.id <http://models.id/> = > events.model_id) > then true > else false > end as did_something > from models; > ``` > > I think something like this will work: > > class Model < Sequel::Model > dataset_module do > def with_did_something > ds = > db[:events].where(:models__id=>:events__model_id).select{max(:events__created_at)} > do_ds = ds.where(:event_type=>'DoSomething') > undo_ds = ds.where(:event_type=>'UndoSomething') > > select_append(Sequel.case( > {Sequel.~(do_ds=>nil) & {undo_ds=>nil} => true, > do_ds > undo_ds => true}, > false > ).as(:did_something)) > end > end > end > > 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] > <mailto:[email protected]>. > To post to this group, send email to [email protected] > <mailto:[email protected]>. > Visit this group at https://groups.google.com/group/sequel-talk > <https://groups.google.com/group/sequel-talk>. > For more options, visit https://groups.google.com/d/optout > <https://groups.google.com/d/optout>. -- 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 https://groups.google.com/group/sequel-talk. For more options, visit https://groups.google.com/d/optout.
