簡體   English   中英

如何對記錄進行分組,然后根據列選擇不同的記錄

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

我試圖按類別對一些記錄進行分組,然后根據別名列選擇所有不同的/唯一的記錄。 就我所知,這是行不通的-它仍然帶來了不同的記錄。

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM