我遇到此查询的问题。我想选择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
答案 0 :(得分: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