On Monday, January 6, 2020 at 5:17:10 AM UTC-8, BeeRich33 wrote:
>
> Hi folks. How do I select three fields from a dataset?
>
> SELECT country, count(id) FROM "thistable" WHERE ("status" = 'active')
> GROUP BY "country" ORDER BY "country" DESC
>
>
> Currently I have:
>
> puts DBS[:thistable].where(:status => 'active').group(:country).order(
> Sequel.desc(:country)).sql
>
>
> So I'm missing the *country* and *count(id) *restrictions. I'm unclear
> how to add those.
>
To select specific columns, use Dataset#select:
DBS[:thistable].
where(:status => 'active').
select{[:country, count(:id)]}.
group(:country).
order(Sequel.desc(:country))
However, Sequel has a shortcut for what you want since it is a common
operation (assuming id is never NULL):
DBS[:thistable].
where(:status => 'active').
group_and_count(:country).
reverse(:country)
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 view this discussion on the web visit
https://groups.google.com/d/msgid/sequel-talk/d0434b8d-ba75-4183-a9c6-84b875dec1fb%40googlegroups.com.