Home » SQL & PL/SQL » SQL & PL/SQL » comparing like fields in same table
comparing like fields in same table [message #6874] Fri, 09 May 2003 08:41 Go to next message
wnc
Messages: 3
Registered: May 2003
Junior Member
I have a table that contains names(formal, preferred, former married, etc). The unique key is called addrkey. There are several entries(rows) for each individual, and the individual is identified by the field nameid. Each name entry(row) has a title(Mr., Mrs., Dr., etc) designated by nametitle. Each name entry is also designated by type (a=preferred, f=formal), which is designated by the field nametype.

What I need to do is find individuals who have different titles in their good and preferred names. For instance, if John Smith's formal title is Dr., and his preferred title is Mr., I want to return his nameid. However, in order to do this, I need to compare rows in the table where the nameid is the same, but nametitle is different, and where the two nametypes being compared are equal to 'a' and 'f'. There are other types of names besides formal and preferred, so there can be more than two rows for each nameid. I need to limit it to preferred and formal.

Thanks in advance,
Will
Re: comparing like fields in same table [message #6878 is a reply to message #6874] Fri, 09 May 2003 11:49 Go to previous messageGo to next message
Mike T
Messages: 32
Registered: August 2002
Member
try this...it assumes you only have 2 types:

select m1.nameid
from mytable m1
where m1.type = 'p' and title <> (select m2.title from mytable m2 where m2.type = 'f' and m1.nameid = m2.nameid)
Re: comparing like fields in same table [message #6881 is a reply to message #6878] Fri, 09 May 2003 13:14 Go to previous messageGo to next message
wnc
Messages: 3
Registered: May 2003
Junior Member
Thanks Mike. That makes perfect sense to me. However, I tried it and it is returning zero records, and I know instances exist. Any ideas? The query runs properly, just returns nada.
Re: comparing like fields in same table [message #6882 is a reply to message #6881] Fri, 09 May 2003 13:34 Go to previous message
nevermind... got it... th
Messages: 1
Registered: May 2003
Junior Member
this did the trick:

select n.nameid from name n, name m
where n.nameid=m.nameid and
n.nametype='a' and n.nametype='f' and
n.nametitle<>m.nametitle
Previous Topic: PL/SQL code to ignore WHEN NO_DATA_FOUND exception
Next Topic: Report
Goto Forum:
  


Current Time: Thu Apr 25 19:17:58 CDT 2024