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

Поиск
Список
Период
Сортировка
От Dimitri Fontaine
Тема Re: How to use an alias name in the current select
Дата
Msg-id m2d3ytwn8h.fsf@hi-media.com
обсуждение исходный текст
Ответ на How to use an alias name in the current select  (Guy Deleeuw <G.De_Leeuw@eurofer.be>)
Ответы Re: How to use an alias name in the current select  (Guy Deleeuw <G.De_Leeuw@eurofer.be>)
Список 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

В списке pgsql-admin по дате отправления:

Предыдущее
От: Ray Stell
Дата:
Сообщение: 8.2.15 pitr/00000001.history
Следующее
От: Ray Stell
Дата:
Сообщение: Re: 8.2.15 pitr/00000001.history