Home » SQL & PL/SQL » SQL & PL/SQL » subquery problem
subquery problem [message #7490] Tue, 17 June 2003 13:30 Go to next message
priya
Messages: 108
Registered: February 2000
Senior Member
I have two tables.

"Machine" table has Primary Key as host_id
-------------------------
host_id
ip
person
group_id
activitydate

"Group" has group_id as the Primary key
----------------------------
group_id
name
description
activity date

The group_id in Machine table is the foreign key for group_id in Group table.

When I add someone in machine.. I can either add a person or a group_id (not both).

Now I want to select everyone in Machine and just the name from Group table who has activity date from 1st Jan 2003 to today. How can I do this? Since some of the people in Machine may not have group..

I want to see all people from Jan to now.. and instead of selecting the group_id .. I want to select their group name..

Please help.
Re: subquery problem [message #7491 is a reply to message #7490] Tue, 17 June 2003 13:44 Go to previous messageGo to next message
Art Metzer
Messages: 2480
Registered: December 2002
Senior Member
I think the outer join is what you're after:
SELECT m.host_id
,      m.ip
,      TO_CHAR(m.activity_date
       ,       'fmMM/DD/YYYY')  activity_date
,      g.name                   group_name
FROM   machine      m
,      group        g
WHERE  m.activity_date BETWEEN TO_DATE('20030101','YYYYMMDD')
                           AND SYSDATE
AND    m.group_id = g.group_id <b><font color=red>(+)</font></b>  
Re: subquery problem [message #7492 is a reply to message #7490] Tue, 17 June 2003 13:51 Go to previous messageGo to next message
Mike T
Messages: 32
Registered: August 2002
Member
You are looking for what's called an Outer Join (if you have Oracle 9, you can write this differently):

select m.person, g.name
from machine m, group g
where m.group_id = g.group_id (+)
and activity date >= '20030101'

I have no idea how your "activity date" field is set up, so you may have to play with the format of the 20030101 part, but this should get you closer to what you want.
Re: subquery problem [message #7495 is a reply to message #7492] Tue, 17 June 2003 14:32 Go to previous messageGo to next message
priya
Messages: 108
Registered: February 2000
Senior Member
Thank you so much !

I tried this query so many time but I did not add the (+) there.. May I ask what the (+) is for?
Re: subquery problem [message #7501 is a reply to message #7495] Wed, 18 June 2003 01:13 Go to previous message
Christo Kutrovsky
Messages: 1
Registered: June 2003
Junior Member
(+) is for outer join.

You can treat it as a "may" . Read everything about outer joins before using it, as there are some non-intutional things.
Previous Topic: How to find sums??????
Next Topic: DBMS.JOB SCHEDULE - HELP
Goto Forum:
  


Current Time: Wed Apr 24 17:45:54 CDT 2024