简体   繁体   中英

Codewars postgres challenge pg::syntaxerror

I'm doing a SQL challenge on Codewars.com. So far so good.

The challenge I'm trying to solve is https://www.codewars.com/kata/calculating-month-over-month-percentage-growth-rate/train/sql

And my SQL looks like:

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

however, the server keeps on returning me the PG::SyntaxError: ERROR: subquery in FROM must have an alias

I'm not an expert with Postgres, but I know that I have alias for date, count, and growth as is expected from the task.

What else am I missing?

Any help is welcome.

Nevermind, I found an issue. I've had an extra bracket in this line:

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

It should have been:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM