Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle Sql NOT NULL xor NULL?

Tags:

sql

oracle

I'm selecting a group of records and I want to filter a column in the logic of XOR - IS NOT NULL xor IS NULL.

--basic
SELECT make, model
FROM cars

results
--------
ford   taurus
ford   (null)
toyota camry
toyota (null)
honda  (null)

--Obviously XOR is not a real Oracle operator
--This is what I'm trying to do..
SELECT make, model
FROM cars
WHERE model IS NOT NULL
  XOR model IS NULL 

results (pulls records where model IS NOT NULL, falling back to NULL if necessary)
--------
ford   taurus
toyota camry
honda  (null)

Can anyone give me insight on how to achieve the desired result I'm looking for? I'm struggling on this one!

Many thanks!

like image 945
John Strickler Avatar asked Apr 29 '11 13:04

John Strickler


2 Answers

SELECT  make, model
FROM    (
        SELECT  c.*,
                ROW_NUMBER() OVER (PARTITION BY make ORDER BY model NULLS LAST) AS rn
        FROM    cars c
        )
WHERE   NOT (rn > 1 AND model IS NULL)
like image 145
Quassnoi Avatar answered Oct 24 '22 02:10

Quassnoi


SELECT make, model
FROM cars 
WHERE model IS NOT NULL
UNION -- Add makes that don't have any specific model
SELECT make, model 
FROM cars 
WHERE make NOT IN 
  (SELECT make
  FROM cars 
  WHERE model IS NOT NULL)
like image 35
Klas Lindbäck Avatar answered Oct 24 '22 01:10

Klas Lindbäck