I am getting this error, the query works in SQLite.
ActiveRecord::StatementInvalid (PG::GroupingError: ERROR: COLUMN "receipts.is_read" must appear IN the GROUP BY clause OR be used IN an aggregate FUNCTION
Unsure what to do, it also occurs when I remove the coalesce part. I am not sure what else I can specify to make it work. Any ideas?
SELECT DISTINCT conversations.id, COALESCE(CASE WHEN receipts.is_read = 't' THEN 't' END, 'f') AS READ, conversations.updated_at, p1.nickname, p2.nickname
FROM conversations
INNER JOIN notifications ON notifications.conversation_id = conversations.id
INNER JOIN receipts ON receipts.notification_id = notifications.id
INNER JOIN profiles p1 ON p1.id = notifications.sender_id
INNER JOIN profiles p2 ON p2.id = receipts.receiver_id
WHERE (receipts.receiver_id = #{SELF.id} ) AND notifications.sender_id != #{SELF.id} OR (notifications.sender_id = #{SELF.id}) AND receipts.receiver_id != #{SELF.id}
GROUP BY conversations.id
ORDER BY conversations.updated_at DESC