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
  ,case
  when (select max(events.created_at) from events where 
event_type='DoSomething' and models.id = events.model_id) is not null
    and
    (select max(events.created_at) from events where event_type='UndoSomething' 
 and models.id = events.model_id) is null
    then true
  when (select max(events.created_at) from events where 
event_type='DoSomething' and models.id = events.model_id)
    >
    (select max(events.created_at) from events where event_type='UndoSomething' 
 and models.id = events.model_id)
    then true
  else false
  end as did_something
from models;
```

In Model I have tried
```
def with_did_something
        select(:models,
                   Sequel.case([
                                                
[select(:events).where(:event_type => ‘DoSomething’, :models__id => 
:events__model_id).max(:events__created_at) 
                                                 >
                                                 
select(:events).where(:event_type => ‘UndoSomething’, :models__id => 
:events__model_id).max(:events__created_at)],
                                                true
                                        ], false).as(:did_something)
                )
end
```
Of course it will not work because `select(:events)` in `dataset_module` will 
become `select events from leads` SQL.
If you can, could you please give me a hint? Thank you

Cheers,
Bang
> On 29 Apr 2016, at 8:03 AM, Jeremy Evans <[email protected]> wrote:
> 
> On Wednesday, April 27, 2016 at 6:13:42 AM UTC-7, The Bang Nguyen wrote:
> Hi Jeremy,
> 
> I thought I got your idea, but I did not.
> Sorry for bothering you again.
> 
> >do_something = event_ds.where(:event_type=>'do_something').max(:created_time)
> If there is any `event` with `event_type` is `do_something`, do_something 
> will always be not nil.
> Thus `did_something` will be always true.
> 
> For example:
> Lets say I have 2 `model`(s).
> First one have `event` with `event_type` is `do_something`. Second one does 
> not have any event.
> With your code, I will receive dataset in which both `did_something` for 2 
> `model` are `true`.
> 
> My expectation is to get back the dataset in which `did_something` for first 
> `model` is `true` and second one is `false`
> 
> Ah, then you'd probably have to select a CASE expression with a subquery 
> using similar logic as my earlier example.  Try giving that a shot.  If you 
> can't get it to work, please post here with your attempt.
> 
> 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.

Reply via email to