sql - Oracle SQL NOT NULL x或NULL?

标签 sql oracle

我正在选择一组记录,并且想按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)

谁能为我提供有关如何获得所需结果的见解?我正在为此奋斗!

非常感谢!

最佳答案

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)

关于sql - Oracle SQL NOT NULL x或NULL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5832473/

相关文章:

mysql - SQL - sum (column 1 + column2 + column3 ) 并在每组中选择总和的最大值

mysql - 将某些字段(仅结构)从另一个表复制到现有表中

sql - 如何在 Oracle 10g 中解析相邻列表?

sql - 为什么 Oracle 隐式地将 SYS_EXTRACT_UTC 添加到索引中?

c# - 当我尝试向 Oracle 插入日期和时间时出现 ORA-01843

sql - MySQL 查询 : I need all "friendships" between different users

SQL - HAVING 与 WHERE

oracle - 如何将 Oracle Database 21c Express 与 DBeaver 连接

oracle - 如何从批量收集的记录表类型中进行选择

sql - Oracle Insert Select with order by