Unix Technical Forum

SQL Question

This is a discussion on SQL Question within the pgsql Novice forums, part of the PostgreSQL category; --> I have a table called 'tasks' with a structure like this: Taskid ParentId ---------------------------- 1 null 2 1 3 ...


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > pgsql Novice

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-17-2008, 10:36 PM
Kevin Crenshaw
 
Posts: n/a
Default SQL Question

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.

Any help would really be appreciated.

Thanks!

Kevin



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-17-2008, 10:36 PM
Sean Davis
 
Posts: n/a
Default Re: SQL Question




On 2/22/06 9:43 AM, "Kevin Crenshaw" <kcrenshaw@viscient.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);




---------------------------(end of broadcast)---------------------------
TIP 2: Don't 'kill -9' the postmaster

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-17-2008, 10:37 PM
Kevin Crenshaw
 
Posts: n/a
Default Re: SQL Question

That did the trick. Thanks for your help!



-----Original Message-----
From: pgsql-novice-owner@postgresql.org
[mailtogsql-novice-owner@postgresql.org] On Behalf Of Sean Davis
Sent: Wednesday, February 22, 2006 10:03 AM
To: Kevin Crenshaw; pgsql-novice@postgresql.org
Subject: Re: [NOVICE] SQL Question




On 2/22/06 9:43 AM, "Kevin Crenshaw" <kcrenshaw@viscient.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);




---------------------------(end of broadcast)---------------------------
TIP 2: Don't 'kill -9' the postmaster


---------------------------(end of broadcast)---------------------------
TIP 3: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faq

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 04:00 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com