Codewars Postgres挑战pg :: syntaxerror

时间:2018-09-11 16:04:16

标签: sql postgresql

我正在Codewars.com上进行SQL挑战。到目前为止,一切都很好。

我要解决的挑战是https://www.codewars.com/kata/calculating-month-over-month-percentage-growth-rate/train/sql

我的SQL看起来像:

select date_trunc('month', created_at)::date as date, 
  count(distinct created_at) as count, 
  100 * (count(*) - lag(count(*), 1) over (order by date)) / lag(count(*), 1) over (order by date)) || '%' as growth
from posts
group by date
order by date asc

但是,服务器不断向我退回PG::SyntaxError: ERROR: subquery in FROM must have an alias

我不是Postgres的专家,但我知道我对任务的期望有日期,计数和增长的别名。

我还想念什么?

欢迎任何帮助。

1 个答案:

答案 0 :(得分:0)

没关系,我发现了一个问题。 我在这一行中有一个额外的括号:

100 * (count(*) - lag(count(*), 1) over (order by date)) / lag(count(*), 1) over (order by date)) || '%' as growth

应该是:

100 * (count(*) - lag(count(*), 1) over (order by date)) / lag(count(*), 1) over (order by date) || '%' as growth