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!
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)
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With