Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need Help With SQL Statement

Status
Not open for further replies.

ronfranks1

Programmer
Apr 14, 2007
1
US
This SQL needs to return 1 NEW record, marked "NEW", and only 1 old record, marked "OLD", if one existed before. The problem is, I am returning multiple OLD records that have a different dates from table2.

This is what I want:

12345 New
12345 Old

This is what I don't want:

12345 New
12345 Old
12345 Old
12345 Old

Here is my script:

SELECT
distinct a.job_number ||'New'
FROM table1 a, table2 b
WHERE a.date = b.date
AND a.job_number = b.job_number;
UNION
SELECT
distinct a.job_number ||'Old'
FROM table1 a, table2 b
WHERE a.date < b.date
AND a.job_number = b.job_number;

Thanks in advance.
 
Code:
SELECT DISTINCT 
       a.job_number ||
        case when a.date = b.date
             then 'New' else 'Old' end
  FROM table1 a
INNER
  JOIN table2 b
    ON b.job_number = a.job_number
   AND b.date >= a.date

r937.com | rudy.ca
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top