2017-02-27 35 views
0

我試圖在Ruby on Rails中查找表中特定SongId的最頻繁標題。這是我到目前爲止有:Rails/SQL:如何找到特定ID上最頻繁的列值

SongDetail.where(song_id: id).group('title').order('COUNT(*) DESC').first[:title] 

我不斷收到讀取錯誤:

PG::GroupingError: ERROR: column "song_details.id" must appear in the GROUP BY clause or be used in an aggregate function

應該如何進行?

回答

0

集團的意志集團與不同列的紀錄,但在這裏查詢的Postgres是混淆選擇從title組這song_details.id,所以從這個以下襬脫被欺騙

SongDetail.where(song_id: id).group('title').order('COUNT(*) DESC').select('title').limit(1) 
+0

也做到了,謝謝! – dcporter7

+0

歡迎@ dcporter7 – ashvin

相關問題