PostgreSQL: using a calculated column in the same query

You need to wrap the SELECT statement into a derived table in order to be able to access the column alias: select cost1, quantity_1, cost_2, quantity_2 total_1 + total_2 as total_3 from ( select cost_1, quantity_1, cost_2, quantity_2, (cost_1 * quantity_1) as total_1, (cost_2 * quantity_2) as total_2 from data ) t There won’t be … Read more

Using an Alias column in the where clause in Postgresql

I struggled on the same issue and “mysql syntax is non-standard” is not a valid argument in my opinion. PostgreSQL adds handy non-standard extensions as well, for example “INSERT … RETURNING …” to get auto ids after inserts. Also, repeating large queries is not an elegant solution. However, I found the WITH statement very helpful … Read more