0

I have a column utc_time where stores unix timestamp,

and I want to group by date,

But got errors.

  def self.memory_info_daily(test_id=1)
    self.unscoped
    .where{ name =~ 'MemoryInfo' }
    .where{ sony_alarm_test_id.eq(test_id)}
    .group("to_char(to_timestamp(utc_time), 'YYYY-MM-DD')")

But it works by append .count in the end of query.

Error

SELECT "sony_alarm_logs".* FROM "sony_alarm_logs" WHERE "sony_alarm_logs"."name" ILIKE 'MemoryInfo' AND "sony_alarm_logs"."sony_alarm_test_id" = 1 GROUP BY to_char(to_timestamp(utc_time), 'YYYY-MM-DD')
E, [2014-06-27T16:17:14.379965 #29797] ERROR -- : PG::GroupingError: ERROR:  column "sony_alarm_logs.id" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: SELECT "sony_alarm_logs".* FROM "sony_alarm_logs"  WHERE "so...
               ^
newBike
  • 14,385
  • 29
  • 109
  • 192
  • 2
    There are tons of questions in SO related to this: http://stackoverflow.com/search?q=column+must+appear+in+the+GROUP+BY+clause+%5Bpostgresql%5D – pozs Jun 27 '14 at 08:28
  • See also http://stackoverflow.com/q/19601948/398670 . Please let me know if I've misunderstood and this isn't the same issue; I'll reopen the question once you edit it to clarify. – Craig Ringer Jun 28 '14 at 04:25

0 Answers0