pg.ddx.io pgsql-admin@postgresql.org mailing list archive
help / color / mirror / Atom feedHow to use an alias name in the current select
3+ messages / 2 participants
[nested] [flat]
* How to use an alias name in the current select
@ 2010-03-24 16:42 Guy Deleeuw <G.De_Leeuw@eurofer.be>
2010-03-24 19:09 ` Re: How to use an alias name in the current select Dimitri Fontaine <dfontaine@hi-media.com>
0 siblings, 1 reply; 3+ messages in thread
From: Guy Deleeuw @ 2010-03-24 16:42 UTC (permalink / raw)
To: pgsql-admin
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
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: How to use an alias name in the current select
2010-03-24 16:42 How to use an alias name in the current select Guy Deleeuw <G.De_Leeuw@eurofer.be>
@ 2010-03-24 19:09 ` Dimitri Fontaine <dfontaine@hi-media.com>
2010-03-25 07:18 ` Re: How to use an alias name in the current select Guy Deleeuw <G.De_Leeuw@eurofer.be>
0 siblings, 1 reply; 3+ messages in thread
From: Dimitri Fontaine @ 2010-03-24 19:09 UTC (permalink / raw)
To: Guy Deleeuw <G.De_Leeuw@eurofer.be>; +Cc: pgsql-admin
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
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: How to use an alias name in the current select
2010-03-24 16:42 How to use an alias name in the current select Guy Deleeuw <G.De_Leeuw@eurofer.be>
2010-03-24 19:09 ` Re: How to use an alias name in the current select Dimitri Fontaine <dfontaine@hi-media.com>
@ 2010-03-25 07:18 ` Guy Deleeuw <G.De_Leeuw@eurofer.be>
0 siblings, 0 replies; 3+ messages in thread
From: Guy Deleeuw @ 2010-03-25 07:18 UTC (permalink / raw)
To: Dimitri Fontaine <dfontaine@hi-media.com>; +Cc: 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.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,
>
^ permalink raw reply [nested|flat] 3+ messages in thread
end of thread, other threads:[~2010-03-25 07:18 UTC | newest]
Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2010-03-24 16:42 How to use an alias name in the current select Guy Deleeuw <G.De_Leeuw@eurofer.be>
2010-03-24 19:09 ` Dimitri Fontaine <dfontaine@hi-media.com>
2010-03-25 07:18 ` Guy Deleeuw <G.De_Leeuw@eurofer.be>
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