I tried to uses aliases in postgres for this query, but postgres stops and complains that ERROR: column “subtotal” does not exist
SELECT SUM(price) AS subtotal,
subtotal * 3.0 AS fees,
(subtotal + fees) AS total
FROM cart
You cannot use aliases as part of your next column?
No, you can not re-use a column alias within the same SQL statement – use:
You can reference the column alias in the ORDER BY clause, some databases also support referencing in the
GROUP BYandHAVINGclauses.