0

我遇到了这个查询的问题。我想选择 ID 字段,然后日期时间戳字段有一个别名,但按别名分组。如果不包含 ID 字段,我将无法做到这一点,但随后它就无法保持 group by 提供的唯一性。这是我的查询不起作用。我收到关于需要包含 ID 字段的错误。

SELECT id, to_timestamp(start_date)::date as sdate FROM events WHERE end_date > 1349306845 GROUP BY sdate ORDER BY sdate ASC LIMIT 20

更新:使用如下功能后,它可以根据需要工作。

   SELECT max(id) as id, to_timestamp(start_date)::date as sdate FROM events WHERE end_date > 1349306845 GROUP BY sdate ORDER BY sdate ASC LIMIT 20
4

1 回答 1

1
SELECT  id, to_timestamp(start_date)::date as sdate 
FROM events
WHERE start_date IN 
   (
     SELECT start_date as sdat 
     FROM events 
     WHERE end_date > 1349306845 
     GROUP BY sdat 
     ORDER BY sdat 
     ASC LIMIT 20 
   )

SELECT  id, to_timestamp(start_date)::date as sdate 
FROM events
WHERE start_date IN 
   (
     SELECT start_date as sdat 
     FROM events 
     WHERE end_date > 1349306845 
     GROUP BY sdat 
   )
ORDER BY sdate 
ASC LIMIT 20 
于 2012-10-04T05:48:06.213 回答