如何对记录进行分组,然后根据列选择不同记录
问题描述:
我试图按类别对某些记录进行分组,然后根据别名列选择所有不同/唯一的记录。这里是我得到的,但它没有工作 - 它仍然带来不明显的记录。如何对记录进行分组,然后根据列选择不同记录
Location.where("calendar_account_id = ?", current_user.calendar_accounts.first).group(:id,:alias).order("alias ASC").distinct.group_by(&:category)
我在做什么错在这里?
答
试试这个,
Location.select("DISTINCT(alias), *").where("calendar_account_id = ?", current_user.calendar_accounts.first).order("alias ASC").group_by(&:category)
或
Location.where("calendar_account_id = ?", current_user.calendar_accounts.first).group(:alias).order("alias ASC").group_by(&:category)