0

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.

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.