using column alias to make operations
От | Raimon Fernandez |
---|---|
Тема | using column alias to make operations |
Дата | |
Msg-id | DEC1A9AA-FBDB-4C28-95E7-19B0FA8AC387@montx.com обсуждение исходный текст |
Ответы |
Re: using column alias to make operations
Multiple rows into one row |
Список | 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 по дате отправления: