using column alias to make operations

From: Raimon Fernandez <coder(at)montx(dot)com>
To: PostgreSQL Novice <pgsql-novice(at)postgresql(dot)org>
Subject: using column alias to make operations
Date: 2008-01-23 08:10:09
Message-ID: DEC1A9AA-FBDB-4C28-95E7-19B0FA8AC387@montx.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Hello,

Given this SQL:

SELECT c.name AS Customer, sum(rc.days_total) AS Days,
count(distinct(rc.id)) AS Cages FROM customers c INNER JOIN
reserved_cages rc ON c.ID=rc.customer_id GROUP BY c.name

Now I want to display the Days/Cages, and I want to use the column
aliases for it, like this:

SELECT c.name AS Customer, sum(rc.days_total) AS Days,
count(distinct(rc.id)) AS Cages, (Days/Cages) FROM customers c INNER
JOIN reserved_cages rc ON c.ID=rc.customer_id GROUP BY c.name

But this doesn't work, but this works:

SELECT c.name AS Customer, sum(rc.days_total) AS Days,
count(distinct(rc.id)) AS Cages, (sum(rc.days_total)/
count(distinct(rc.id))) AS AVG FROM customers c INNER JOIN
reserved_cages rc ON c.ID=rc.customer_id GROUP BY c.name

But I think this is double-time as It has to make twice the same
operations.

I thought I could use the column alias, because in the ORDER BY
clausule I can use them ...

thanks !

raimon

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Andrej Ricnik-Bay 2008-01-23 08:24:23 Re: Need help with a function boolean --> text or array
Previous Message Vyacheslav Kalinin 2008-01-23 05:09:20 Re: Need help with a function boolean --> text or array