Skip site navigation (1) Skip section navigation (2)

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

From: Guy Deleeuw <G(dot)De_Leeuw(at)eurofer(dot)be>
To: Dimitri Fontaine <dfontaine(at)hi-media(dot)com>
Cc: pgsql-admin(at)postgresql(dot)org
Subject: Re: How to use an alias name in the current select
Date: 2010-03-25 07:18:43
Message-ID: 4BAB0E53.8020303@eurofer.be (view raw or flat)
Thread:
Lists: pgsql-admin
Hello Dimitri,

Many thanks for your answers

Regards
Guy

Le 24/03/10 20:09, Dimitri Fontaine a écrit :
> Hi,
>
> Guy Deleeuw<G(dot)De_Leeuw(at)eurofer(dot)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,
>    

In response to

pgsql-admin by date

Next:From: Josef SpringerDate: 2010-03-25 08:37:16
Subject: Can not access PostgreSQL 8.8 via ADO
Previous:From: Bruce MomjianDate: 2010-03-24 21:44:20
Subject: Re: 8.2.15 pitr/00000001.history

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group