using column alias to make operations - Mailing list pgsql-novice

From Raimon Fernandez
Subject using column alias to make operations
Date
Msg-id DEC1A9AA-FBDB-4C28-95E7-19B0FA8AC387@montx.com
Whole thread Raw
Responses Re: using column alias to make operations  ("A. Kretschmer" <andreas.kretschmer@schollglas.com>)
Multiple rows into one row  (Raimon <coder@montx.com>)
List 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


pgsql-novice by date:

Previous
From: "Vyacheslav Kalinin"
Date:
Subject: Re: Need help with a function boolean --> text or array
Next
From: "Andrej Ricnik-Bay"
Date:
Subject: Re: Need help with a function boolean --> text or array