我的PG查询:
SELECT "Tracks"."PageId",
date_trunc("month", "Tracks"."createdAt") AS month,
count(*)
FROM "Tracks"
WHERE "Tracks"."PageId" IN (29,30,31)
GROUP BY month, "Tracks"."PageId"
和我的架构:
id, createdAt, updatedAt, PageId
为什么我收到这个错误有点困惑!
答案 0 :(得分:1)
您可以在where或group by子句中使用别名。您需要重复表达式:
SELECT "Tracks"."PageId",
date_trunc('month', "Tracks"."createdAt") AS month,
count(*)
FROM "Tracks"
WHERE "Tracks"."PageId" IN (29,30,31)
GROUP BY date_trunc('month', "Tracks"."createdAt"), "Tracks"."PageId";
请注意,date_trunc()
的第一个参数是varchar
值,因此您需要将其放在单引号中,而不是双引号。
如果您不想重复表达式,可以将其放入派生表中:
select "PageId", month, count(*)
from (
SELECT "Tracks"."PageId",
date_trunc('month', "Tracks"."createdAt") AS month
FROM "Tracks"
WHERE "Tracks"."PageId" IN (29,30,31)
) t
group by month, "PageId";
无关,但是:你应该真的避免引用标识符。他们是值得的,他们更麻烦