From: Dimitri Fontaine <dfontaine@hi-media.com>
To: Guy Deleeuw <G.De_Leeuw@eurofer.be>
Cc: pgsql-admin@postgresql.org
Subject: Re: How to use an alias name in the current select
Date: Wed, 24 Mar 2010 20:09:18 +0100
Message-ID: <m2d3ytwn8h.fsf@hi-media.com> (raw)
In-Reply-To: <4BAA40E2.3000805@eurofer.be>
References: <4BAA40E2.3000805@eurofer.be>
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,
--
dim
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: dfontaine@hi-media.com, G.De_Leeuw@eurofer.be
Subject: Re: How to use an alias name in the current select
In-Reply-To: <m2d3ytwn8h.fsf@hi-media.com>
* 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