On Tuesday, May 29, 2018 at 9:23:54 AM UTC-7, Jason Landry wrote:
>
> I am working on a query that has case statement that gives values based
> on the age of a field. I have it working, but I just want to see if this
> is the best way to accomplish it.
>
> Sequel.case([
> [Sequel.expr {Sequel.function(:age, :last_seen) <=
> Sequel.lit("interval '1 months'")}, 10],
> [Sequel.expr {Sequel.function(:age, :last_seen) <=
> Sequel.lit("interval '3 months'")}, 5]
> ], 0)
>
> I'm giving 10 points to last_seen less than 1 month ago, 5 points less
> than 3 months ago, and zero points otherwise. The generated case statement
> would look something like:
>
> case
> when age(last_seen) <= interval '1 month' then 10
> when age(last_seen) <= interval '3 months' then 5
> else 0
> end
>
> I would prefer to eliminate the Sequel.lit("interval..") method calls if
> I can (is this something the pg_interval plugin would handle).
>
> Does this seem like the best approach?
>
Sequel.cast('1 months', :interval) instead of the Sequel.lit call should
work. If you are going to use Sequel.expr, you can make it shorter:
Sequel.case([
[Sequel.expr {age(:last_seen) <= Sequel.cast('1 months', :interval)},
10],
[Sequel.expr {age(:last_seen) <= Sequel.cast('1 months', :interval)},
5]
], 0)
Alternatively, you just drop the Sequel.expr:
Sequel.case([
[Sequel.function(:age, :last_seen) <= Sequel.cast('1 months',
:interval), 10],
[Sequel.function(:age, :last_seen) <= Sequel.cast('1 months',
:interval), 5]
], 0)
The general approach seems good to me.
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 https://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.