views:

32

answers:

1

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?

+1  A: 

No, you can not re-use a column alias within the same SQL statement - use:

SELECT SUM(t.price) AS subtotal,
       SUM(t.price) * 3.0 AS fees,
       SUM(t.price + fees) AS total
  FROM CART t

You can reference the column alias in the ORDER BY clause, some databases also support referencing in the GROUP BY and HAVING clauses.

OMG Ponies