Skip to content
Advertisement

trying to sum a column in postgres but trying to limit the results first?

I am trying to get data for the last 5 years within my database, then sum the results. Here’s what I’ve been trying:

but I’m getting this error:

when I comment out order by year desc, then the query works but I need it to start at the most recent year. also the limit doesn’t seem to do anything when the order by year is missing.

Is there a way I can do this?

Advertisement

Answer

move the limit to a subquery. The subquery only returns the first 5 tuples. Then continue your processing as normal.

Example:

So this is probably the answer to your query:

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement