pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Guy Deleeuw <G.De_Leeuw@eurofer.be>
To: Dimitri Fontaine <dfontaine@hi-media.com>
Cc: pgsql-admin@postgresql.org
Subject: Re: How to use an alias name in the current select
Date: Thu, 25 Mar 2010 08:18:43 +0100
Message-ID: <4BAB0E53.8020303@eurofer.be> (raw)
In-Reply-To: <m2d3ytwn8h.fsf@hi-media.com>
References: <4BAA40E2.3000805@eurofer.be>
	<m2d3ytwn8h.fsf@hi-media.com>

Hello Dimitri,

Many thanks for your answers

Regards
Guy

Le 24/03/10 20:09, Dimitri Fontaine a écrit :
> Hi,
>
> Guy Deleeuw<G.De_Leeuw@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,
>    



view thread (3+ messages)

Message-ID: <4BAB0E53.8020303@eurofer.be>
Permalink:  ../4BAB0E53.8020303@eurofer.be/
Also on:    postgresql.org/message-id/4BAB0E53.8020303@eurofer.be

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-admin@postgresql.org
  Cc: G.De_Leeuw@eurofer.be, dfontaine@hi-media.com
  Subject: Re: How to use an alias name in the current select
  In-Reply-To: <4BAB0E53.8020303@eurofer.be>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox