How to use an alias name in the current select

Hello

I have :
SELECT
t_orders_articles.k_id AS k_id
,t_orders_articles.quantity AS order_qty
,(SELECT sum(quantity) FROM t_deliveries_articles WHERE
article_id = t_orders_articles.k_id) AS delivery_qty
, (t_orders_articles.quantity - delivery_qty) AS qty
FROM t_orders_articles
......;

But it's not working I receive the error :
ERROR : The column delivery_qty does not exist.

How can I make that without having 2 select call ?

Regards
Guy


--
Sent via pgsql-admin mailing list (pgsql-admin [at] postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-admin
De Leeuw Guy [ Mi, 24 März 2010 17:42 ] [ ID #2036227 ]

Re: How to use an alias name in the current select

Hi,

Guy Deleeuw <G.De_Leeuw [at] eurofer.be> writes:
> SELECT
> t_orders_articles.k_id AS k_id
> ,t_orders_articles.quantity AS order_qty
> ,(SELECT sum(quantity) FROM t_deliveries_articles WHERE article_id =
> t_orders_articles.k_id) AS delivery_qty
> , (t_orders_articles.quantity - delivery_qty) AS qty
> FROM t_orders_articles
> ......;
>
> How can I make that without having 2 select call ?

Put it as a relation in a subquery, or use WITH if using 8.4.

SELECT k_id, order_qty, quantity, delivery_qty,
quantity - delivery_qty as qty
FROM (
SELECT
t_orders_articles.k_id AS k_id
,t_orders_articles.quantity AS order_qty
,(SELECT sum(quantity) FROM t_deliveries_articles WHERE article_id =
t_orders_articles.k_id) AS delivery_qty
, t_orders_articles.quantity
FROM t_orders_articles
) as t;


Or

WITH t AS (
SELECT
t_orders_articles.k_id AS k_id
,t_orders_articles.quantity AS order_qty
,(SELECT sum(quantity) FROM t_deliveries_articles WHERE article_id =
t_orders_articles.k_id) AS delivery_qty
, t_orders_articles.quantity
FROM t_orders_articles
)
SELECT k_id, order_qty, quantity, delivery_qty,
quantity - delivery_qty as qty
FROM t;

Regards,
--
dim

--
Sent via pgsql-admin mailing list (pgsql-admin [at] postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-admin
Dimitri Fontaine [ Mi, 24 März 2010 20:09 ] [ ID #2036229 ]

Re: How to use an alias name in the current select

Hello Dimitri,

Many thanks for your answers

Regards
Guy

Le 24/03/10 20:09, Dimitri Fontaine a =E9crit :
> Hi,
>
> Guy Deleeuw<G.De_Leeuw [at] eurofer.be> writes:
>
>> SELECT
>> t_orders_articles.k_id AS k_id
>> ,t_orders_articles.quantity AS order_qty
>> ,(SELECT sum(quantity) FROM t_deliveries_articles WHERE artic=
le_id =3D
>> t_orders_articles.k_id) AS delivery_qty
>> , (t_orders_articles.quantity - delivery_qty) AS qty
>> FROM t_orders_articles
>> ......;
>>
>> How can I make that without having 2 select call ?
>>
> Put it as a relation in a subquery, or use WITH if using 8.4.
>
> SELECT k_id, order_qty, quantity, delivery_qty,
> quantity - delivery_qty as qty
> FROM (
> SELECT
> t_orders_articles.k_id AS k_id
> ,t_orders_articles.quantity AS order_qty
> ,(SELECT sum(quantity) FROM t_deliveries_articles WHERE artic=
le_id =3D
> t_orders_articles.k_id) AS delivery_qty
> , t_orders_articles.quantity
> FROM t_orders_articles
> ) as t;
>
>
> Or
>
> WITH t AS (
> SELECT
> t_orders_articles.k_id AS k_id
> ,t_orders_articles.quantity AS order_qty
> ,(SELECT sum(quantity) FROM t_deliveries_articles WHERE artic=
le_id =3D
> t_orders_articles.k_id) AS delivery_qty
> , t_orders_articles.quantity
> FROM t_orders_articles
> )
> SELECT k_id, order_qty, quantity, delivery_qty,
> quantity - delivery_qty as qty
> FROM t;
>
> Regards,
>

--
Sent via pgsql-admin mailing list (pgsql-admin [at] postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-admin
De Leeuw Guy [ Do, 25 März 2010 08:18 ] [ ID #2036517 ]
Datenbanken » gmane.comp.db.postgresql.admin » How to use an alias name in the current select

Vorheriges Thema: command to check the database size ?
Nächstes Thema: 8.2.15 pitr/00000001.history