• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 452
  • Last Modified:

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??
0
notbobj1
Asked:
notbobj1
  • 7
  • 2
  • 2
  • +3
1 Solution
 
Nathan RileyFounder/CTOCommented:
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 RileyFounder/CTOCommented:
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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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
 
sujith80Commented:
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
 
sujith80Commented:
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
 
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 7
  • 2
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now