I want to create a pivot table view showing month on month sum of bookings for every travel_mode.
Table bookings:
timestamp , bookings , provider_id Table providers:
provider_id , travel_mode Pivot table function and crosstab functions are not to be used to do this. So I am trying to use JOIN and CASE. Following is the query:
SELECT b.month, (CASE WHEN p.travel_mode=train then b.amount end)train, (CASE WHEN p.travel_mode=bus then b.amount end)bus, (CASE WHEN p.travel_mode=air then b.amount end)air FROM (SELECT to_char(date_,month) as month, travel_mode, sum(bookings) as amount from bookings as b join providers as p on b.provider_id=p.provider_id group by b.month, p.travel_mode) group by b.month; However I am getting an error which says:
subquery in FROM must have an alias LINE 6:
And when I add an alias it throws an error saying:
column p.travel_mode must appear in the GROUP BY clause or be used in an aggregate function LINE 2:
The final result should be something like this
Month Air Bus Train 01 Amount(air) Amount(Bus) Amount(train) I have a feeling it is a minor error somewhere but I am unable to figure it out at all.
P.S. I had to remove all quotations in the question as it was not allowing me to post this. But those are being taken care of in the actual query.
CREATE TABLEstatement) showing data types and constraints and your version of Postgres.