错误:列 access_log.id 必须出现在GROUP BY子句中,或者在使用子查询时用于聚合函数中
select
to_char(date_trunc('day',create_time),'DD MON, YYYY') as create_time,
to_char((max(create_time) - min(create_time)),'HH24:mi') as time_spent,
id
from
access_log
group by
user_id, actionlink_id, date_trunc('day',create_time)
答案 0 :(得分:0)
access_log.id
对于该表中的每一行都是唯一的,因此您不太可能获得有用的信息,包括查询中的信息。我相信您打算通过user_id
执行此操作。但是,错误消息告诉您实际情况, IF 您在id
中包含select clause
,它也应该出现在group by
子句中。继续阅读:
将select clause
中的每个项目视为两种类型:
MIN
MAX
COUNT
AVG
和类似功能的date_trunc('day',create_time)
包含在group by
子句select user_id ---- changed this to user_id , actionlink_id --- & added this , to_char(date_trunc('day',create_time),'DD MON, YYYY') as create_time , to_char((max(create_time) - min(create_time)),'HH24:mi') as time_spent , MAX(id) as max_id from access_log group by -- all non-aggregating select clause items go here user_id , actionlink_id , date_trunc('day',create_time)