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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Retrieve records if they meet a certain criteria (1 record is <1000 and the other one >=1000) 2

Status
Not open for further replies.

kernal

Technical User
Feb 27, 2001
415
US
Two tables

COMBINED

term
id
number


CLASS

term
dept
catalog
number


DATA IN COMBINED

term id number
1154 5555 10121
1154 5555 9587
1154 44444 5698
1154 44444 99999

DATA IN CLASS

term dept catalog number
1154 ART 1000 10121
1154 ART 101 9587
1154 ART 2000 5698
1154 ART 3000 99999

WHAT data needs to be retrieved

I only want data retrieved for records if they have the same combined.id and 1 record has a catalog# less than 1000 and 1 record has a catalog# greater or equal to 1000 so for the above data only the following is retrieved since combined.id 5555 meets the criteria. combined.id 44444 does not meet the criteria because both records have catalog#s greater or equal to 1000

term dept catalog id number
1154 ART 1000 5555 10121
1154 ART 101 5555 9587

Help is VERY appreciated.
 
This will return the combined row where there is one and only one row in class where the catalog is below 1000 and there is one and only one row in class where the catalog is greater then or equal to 1000.

Code:
SELECT *
  FROM Combined A
 WHERE     EXISTS
               (  SELECT Catalog
                    FROM Class B
                   WHERE A.Term = B.Term AND B.Catalog < 1000
                GROUP BY Catalog
                  HAVING COUNT (*) = 1)
       AND EXISTS
               (  SELECT Catalog
                    FROM Class B
                   WHERE A.Term = B.Term AND B.Catalog >= 1000
                GROUP BY Catalog
                  HAVING COUNT (*) = 1);

Bill
Lead Application Developer
New York State, USA
 
Code:
SELECT ca.term, ca.dept, ca.catalog, c1.cnumber
FROM  class ca
      INNER JOIN combined c1
         ON ca.cnumber = c1.cnumber
            AND ca.term = c1.term
      INNER JOIN combined c2
         ON c1.id = c2.id
            AND c1.term = c2.term
      INNER JOIN class cb
         ON c2.cnumber = cb.cnumber
            AND c2.term = cb.term
WHERE (ca.catalog < 1000 AND cb.catalog >= 1000)
      OR (cb.catalog < 1000 AND ca.catalog >= 1000)

Please note that where you are calling for a column called "number", I have used cnumber. Oracle won't let me create a table with column named after a reserved word.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top