简体   繁体   English

如何对记录进行分组,然后根据列选择不同的记录

[英]How to group records and then select distinct records based on column

I am trying to group some records by category and then select all distinct/unique records based on alias column. 我试图按类别对一些记录进行分组,然后根据别名列选择所有不同的/唯一的记录。 Here is as far as i got but its not working - it still brings non distinct records. 就我所知,这是行不通的-它仍然带来了不同的记录。

Location.where("calendar_account_id = ?", current_user.calendar_accounts.first).group(:id,:alias).order("alias ASC").distinct.group_by(&:category)

What am I doing wrong here? 我在这里做错了什么?

Try this, 尝试这个,

Location.select("DISTINCT(alias), *").where("calendar_account_id = ?", current_user.calendar_accounts.first).order("alias ASC").group_by(&:category)

or 要么

Location.where("calendar_account_id = ?", current_user.calendar_accounts.first).group(:alias).order("alias ASC").group_by(&:category)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM