Re: dumb question

From: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
To: Dann Corbit <DCorbit(at)connx(dot)com>
Cc: Steve Clark <steve(dot)clark(at)netwolves(dot)com>, pgsql <pgsql-general(at)postgresql(dot)org>
Subject: Re: dumb question
Date: 2016-06-02 20:16:13
Message-ID: CAKFQuwZ434AwwCUUBsG=NvTn+z+owZU_X0yv3dvGfS=A=h7yXw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

> -----Original Message-----
> From: pgsql-general-owner(at)postgresql(dot)org [mailto:
> pgsql-general-owner(at)postgresql(dot)org] On Behalf Of Steve Clark
> Sent: Thursday, June 2, 2016 9:56 AM
> To: pgsql <pgsql-general(at)postgresql(dot)org>
> Subject: [GENERAL] dumb question
>
> Hi List,
>
> I am a noob trying to do something that seems like it should be easy but I
> can't figure it out.
>
> I have a table like so:
>
> id | ref_id | sts
> ------------------
> 1 | | 0
> 2 | 1 | 1
> 3 | | 0
> 4 | | 0
> 5 | 4 | 1
> 6 | | 0
> 7 | 6 | 1
>
> I want to find the max(id) whose sts is 0 but whose id is not referenced
> by ref_id.
>

On Thu, Jun 2, 2016 at 4:07 PM, Dann Corbit <DCorbit(at)connx(dot)com> wrote:

> This is your request, translated directly into SQL
>
> select max(id) from sometable where sts=0 and ref_id IS NULL
>
> Looking at your sample, it seems that sts is always 1 when ref_id exists,
> so it may possibly simplify to:
>
> select max(id) from sometable where sts=0
>
>
​Please don't top-post.

​Your query would select "id=6", which is disqualified due to id=7...

For the record one reads: "whose id is not referenced by ref_id" AS "id
NOT IN (ref_ids....)"; ref_id IS NULL means "that lacks a ref_id" and is
evaluated independent of the id.

David J.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message David G. Johnston 2016-06-02 20:23:41 Re: dumb question
Previous Message Steve Clark 2016-06-02 20:13:34 Re: dumb question