我正在选择一组记录,我想在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)
有谁能让我了解如何达到我想要的结果?我在这个上苦苦挣扎!
非常感谢!
答案 0 :(得分:12)
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)
答案 1 :(得分:3)
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)
答案 2 :(得分:2)
我最初赞成KlasLindbäck的回答,但现在我想知道这是否会产生预期的结果:
SELECT make, model
FROM Cars
WHERE model IS NOT NULL
UNION
SELECT make, NULL
FROM Cars
MINUS
SELECT make, NULL
FROM cars
WHERE model IS NOT NULL;