sql - 比较可以为 NULL 的多个列
问题描述
我想比较一组列并决定:
- 如果所有非 NULL 的列都相等
- 如果任何非 NULL 列不相等
- 如果所有列都为 NULL
示例输入和输出:
Inn: 'NO', NULL, 'GB', 'NO', 'NO'
Out: 1) FALSE; 2) TRUE; 3) FALSE
----
Inn: 'NO', NULL, 'NO', 'NO', 'NO'
Out: 1) TRUE; 2) FALSE; 3) FALSE
----
Inn: NULL, NULL, NULL, NULL, NULL
Out: 1) FALSE; 2) FALSE; 3) TRUE
我需要测试的列数很多,所以比较每一个的逻辑语句有点麻烦。
系统:Teradata SQL 数据库
解决方案
您将使用case
表达式。也许:
select (case when a is null and b is null and c is null and d is null
then 'all null'
when coalesce(a, b, c, d) = coalesce(b, c, d, a) and
coalesce(a, b, c, d) = coalesce(c, d, b, a) and
coalesce(a, b, c, d) = coalesce(d, a, b, c)
then 'all non-nulls are equal'
else 'unequal'
end);
推荐阅读
- python - 如何使用装饰器访问请求和响应?
- javascript - 使用 webpack 获取 'Uncaught TypeError: $(...).tablesorter is not a function'
- r - R:将多个字符串更改为另一个字符串
- plot - 如何在 Visual Studio 代码中使这个 Plot 更小?
- java - Java Selenium 下拉菜单选项
- c++ - 类型名称 int 的概念
- node.js - 将API获取的PDF直接上传到NodeJS中的s3
- python-3.x - 如何在产品循环中调用元素
- mysql - 如何在 MySQL 5.7 中将 WHERE IN 与空值一起使用?
- javascript - 在下拉菜单中查找选项文本并设置选定和更改文本