Re: SQL Question

From: Sean Davis <sdavis2(at)mail(dot)nih(dot)gov>
To: Kevin Crenshaw <kcrenshaw(at)viscient(dot)com>, <pgsql-novice(at)postgresql(dot)org>
Subject: Re: SQL Question
Date: 2006-02-22 15:03:25
Message-ID: C021E76D.6291%sdavis2@mail.nih.gov
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

On 2/22/06 9:43 AM, "Kevin Crenshaw" <kcrenshaw(at)viscient(dot)com> wrote:

> I have a table called 'tasks' with a structure like this:
>
> Taskid ParentId
> ----------------------------
> 1 null
> 2 1
> 3 1
> 4 Null
> 5 4
> 6 4
> 7 Null
> 8 Null
> 9 15
> 10 18
>
>
> The taskid column is the primary key and the parentid column references
> values in the taskid column. However, it is possible for the parentid to be
> a value that is not found in the taskid column (hence my question.). I need
> to write a query that will retrieve all rows where the parentid is either
> 'null' or does not exist in the taskid column.

How about:

Select * from tasks where parentid is null or parentid not in (select
distinct(taskid) from tasks);

In response to

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message A. Kretschmer 2006-02-22 15:05:21 Re: drop index
Previous Message Christoph Frick 2006-02-22 15:00:31 Re: drop index