Skip to content
Advertisement

Creating a percentage column to a table that is not in the From clause through PostgreSQL

I am working with a bike share dataset located here and here is a sample month of it. I want to get the percent of rides per day per membership type, so I applied this query through PostgreSQL:

The query returned all the fields correct, except the Trips_percent it is all zeros!

I searched through several solutions; I found one suggesting to define the sum first through WITH clause, then use it in the query as follows:

Unfortunately it gave me the following error:

Is it because the FROM clause in the main query does not refer to the table of the WITH query?

What is wrong with either query and how can I do to solve them?

Advertisement

Answer

I would suggest phrasing this as a window function. But the key is avoiding integer division:

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