Select not returning proper row

I have 2 tables
first table has primary key on id column and also has a name column.
second table has primary key on id. It also has date and subject columns.
Example of table 1 data:
name:
123, jim
223, bob
333, dave

subject:
123, 02032008, test1
123, 02042008, test1
123, 02052008, test1
223, 02062008, test1
333, 02062008, test1

I need to write a query that will return the name, date and subject.
That I have no problem doing.
However, I also need to return the name, even if there is no record in table 2 for the user and date provided.
I tried an outer join, a not exists, a not in, etc... and just can't seem to get it figured out.

Here is my select statement:
select distinct(name_name), subject_date, subject_subject from name, subject where name_id=subject_id(+) and name_id = 123 and (subject_date = 02062008 or name_id not in (select distinct(subject_id) from subject where subject_date = '02062008'));

What I get back is 3 rows:
jim, 02032008, test1
jim, 02042008, test1
jim, 02052008, test1

What I am looking to get back is 1 row with jim and no date or subject??
notbobj1Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Nathan RileyFounderCommented:
Here you go.
select distinct(name_name), 
		subject_date, 
		subject_subject 
	from name n
	left outer join subject s on n.name_id = s.subject_id
	where name_id = 123 

Open in new window

0
Nathan RileyFounderCommented:
Not sure of your table names or columns so maybe it needs to be this:
select distinct(n.name), 
                s.date, 
                s.subject 
        from name n
        left outer join subject s on n.name_id = s.subject_id
        where name_id = 123 

Open in new window

0
UnifiedISCommented:
When you include a where clause on your outer joined table, you essentially make it an inner join.  The "where" date condition needs to be applied only to your outer table so add it to the join condition as shown:


SELECT DISTINCT A.Name, B.Date, B.Subject
FROM Table1 A
LEFT OUTER JOIN Table2 B
      ON B.ID = A.ID
      AND B.Date = '02062008'
WHERE A.ID = 123


0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

notbobj1Author Commented:
UnifiedIS - this is very close - I copied your query exactly and ran it.
it is returning 2 rows...
The old way was returning:
jim, 02032008, test1
jim, 02042008, test1
jim, 02052008, test1


now it is returning
jim
jim

I expected it to return just jim (no other data - so that is working great, but I expected only one row). Am I missing something??

Also, can you do the same with a (+) instead of calling out a Left Outer Join??
0
suredazzleCommented:
Hi,

I got one too. My tables are diff.

Jones return, the rest is null.

 Hope it helps!

SELECT DISTINCT(e.ename) NAME, e.hiredate SUBJECT_DATE, m.ename SUBJECT
FROM emp e  
LEFT OUTER JOIN emp m ON m.empno = e.mgr
AND e.empno = 7788;
=============================================================
NAME         SUJECT_DA SUBJECT
---------- --------- ----------
ADAMS         12-JAN-83
ALLEN         20-FEB-81
BLAKE         01-MAY-81
CLARK         09-JUN-81
FORD         03-DEC-81
JAMES         03-DEC-81
JOHN SMITH 01-JAN-02
JONES         02-APR-81
KING         17-NOV-81
MARTIN         28-SEP-81
SCOTT         09-DEC-82 JONES

NAME         SUJECT_DA SUBJECT
---------- --------- ----------
SMITH         17-DEC-80
TURNER         08-SEP-81
WARD         22-FEB-81

14 rows selected.
0
suredazzleCommented:
Here is 1 row.....

SELECT DISTINCT(e.ename) NAME, e.hiredate, m.ename MANAGER
FROM emp e
LEFT OUTER JOIN emp m  ON m.empno = e.mgr
WHERE e.empno = 7788;
===============================

NAME         HIREDATE  MANAGER
---------- --------- ----------
SCOTT         09-DEC-82 JONES

0
suredazzleCommented:
If you want no SUBJECT  OR DATE.

You need 'SUBJECT IS NULL'.
0
suredazzleCommented:
Replace hiredate to "subject_date" & mgr to "name_id"

SELECT DISTINCT(e.ename) NAME, e.hiredate, m.ename MANAGER
FROM emp e
LEFT OUTER JOIN emp m ON m.empno = e.mgr
WHERE e.mgr IS NULL AND e.ename = 'KING';

NAME         HIREDATE  MANAGER
---------- --------- ----------
KING         17-NOV-81
0
suredazzleCommented:
Hi notbobj1,

You put the conditional OR, it returns 2 or more rows.

It should be AND.
0
SujithData ArchitectCommented:
Is this what you are looking at?
SQL> select * from subject;
 
SUBJECT_ID SUBJECT_DATE         SUBJECT_SUBJECT
---------- -------------------- --------------------
       123 02032008             test1
       123 02042008             test1
       123 02052008             test1
       223 02062008             test1
       333 02062008             test1
 
SQL> select name_name , subject_date, subject_subject 
  2  from name n, subject s
  3  where name_id = 123
  4  and name_id = subject_id(+)
  5  and nvl(subject_date(+), '99999999') = '02062008';
 
NAME_NAME            SUBJECT_DATE         SUBJECT_SUBJECT
-------------------- -------------------- --------------------
jim

Open in new window

0
SujithData ArchitectCommented:
Actually that NVL is not required.
select name_name , subject_date, subject_subject 
from name n, subject s
where name_id = 123
and name_id = subject_id(+)
and subject_date(+) = '02062008';

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bhavesh_joshiCommented:
if the second table has primary key on id, how can it have two rows of same id?
0
suredazzleCommented:


2nd table has foreign key of subject id, which it related to "name_id".

It shows the relationship between 2 tables based on these columns. Of course, you can call the 2 columns with different names.
0
suredazzleCommented:
Replace last line, WHERE e.mgr IS NULL OR m.ename = 'KING';

There is no manager with same ID returns twice.

KING is NULL, he is a CEO. ;)

NAME            EMPNO HIREDATE      MANAGER
---------- ---------- --------- ----------
BLAKE             7698 01-MAY-81 KING
CLARK             7782 09-JUN-81 KING
JONES             7566 02-APR-81 KING
KING             7839 17-NOV-81
0
notbobj1Author Commented:
Thanks for all your posts on that. It is exactly what I needed!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Oracle Database

From novice to tech pro — start learning today.