pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: negora <public@negora.com>
To: pgsql-general@lists.postgresql.org
Subject: Re: Get more columns from a lookup type subselect
Date: Fri, 10 Mar 2023 09:43:11 +0100
Message-ID: <906d7125-2de3-bcb9-6a64-c0f58d8a0017@negora.com> (raw)
In-Reply-To: <CAEcMXhmOsronM4SMZT0k8E-zXBvTZGRxU3ryTgwaC1R22Dz5cA@mail.gmail.com>
References: <CAEcMXhmOsronM4SMZT0k8E-zXBvTZGRxU3ryTgwaC1R22Dz5cA@mail.gmail.com>

Hi Chris:

You can use a subselect in the `from` and `join` clauses. That's how I 
get multiple columns from a single subselect.

If the subselect needs to use a reference from the outer scope (i.e. the 
main query) , you can use the `lateral` modifier.

Best regards.


On 10/03/2023 08:34, Durumdara wrote:
> Dear Members!
>
> I use the subselects many times in my Queries to get some info (Name, 
> etc) from a subtable.
>
> Sometimes I have to use it to get the last element.
>
> select t.*,
>    (
>
>     select value from u join ... where ...
>
>     order by id desc limit 1
>
>    ) as last_value,
>
> It is ok, but how can I get more values from subselect without 
> repeating the subquery?
>
> select t.*,
>    (
>
>     select value from u join ... where ...
>
>     order by date desc limit 1
>
>    ) as last_value,
>    (
>
>     select type from u join ... where ...
>
>     order by date desc limit 1
>
>    ) as last_type,
>
> This is not too comfortable, and may make mistakes if the join is not 
> defined properly or the date has duplicates.
>
> Ok, I can use WITH Query:
>
> with
>   pre as ( select * from t .... )
>   ,sub as (select pre.*, (select u.id <http://u.id; from u where ... 
> limit 1) as last_u_id
> select  sub.*, u.value, u.type, u.nnn from sub
> left join u on (u.id <http://u.id; = sub.last_u_id)
>
> But sometimes it makes the Query very long (because I have to read 
> more subselects).
>
> Do you know a simple method for this, like:
>
> select t.*,
>    (
>
>     select value, type, anyfield from u join ... where ...
>
>     order by date desc limit 1
>
>    ) as last_value, last_type, anyfield
>
> ?
>
> Thank you for the help!
>
> Best regards
> Chris

view thread (4+ messages)  latest in thread

Message-ID: <906d7125-2de3-bcb9-6a64-c0f58d8a0017@negora.com>
Permalink:  ../906d7125-2de3-bcb9-6a64-c0f58d8a0017@negora.com/
Also on:    postgresql.org/message-id/906d7125-2de3-bcb9-6a64-c0f58d8a0017@negora.com

 · 

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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: public@negora.com
  Subject: Re: Get more columns from a lookup type subselect
  In-Reply-To: <906d7125-2de3-bcb9-6a64-c0f58d8a0017@negora.com>
  References: <CAEcMXhmOsronM4SMZT0k8E-zXBvTZGRxU3ryTgwaC1R22Dz5cA@mail.gmail.com> <906d7125-2de3-bcb9-6a64-c0f58d8a0017@negora.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