oracle:计算一行中空字段的数量
我有一个有 5 个“可选”字段的表。我想知道有多少行全部 5 个为空,有多少行有 1 个字段非空,等等。
我尝试了一些方法,例如:
select
count(*),
( (if field1 is null then 1 else 0) +
(if field2 is null then 1 else 0) +
etc.
但这当然行不通。
理想情况下,我正在寻找类似的输出
Nulls Cnt
0 200
1 345
...
5 40
是否有一个优雅的解决方案?
I have a table that has 5 "optional" fields. I'd like to find out how many rows have all 5 null, how many have 1 field non-null, etc.
I've tried a couple of things, like:
select
count(*),
( (if field1 is null then 1 else 0) +
(if field2 is null then 1 else 0) +
etc.
but of course that doesn't work.
Ideally, I'm looking for output that's something like
Nulls Cnt
0 200
1 345
...
5 40
Is there an elegant solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
关键字不是
if
,而是case
,并且必须使用end
结束case
语句。这是一个适合您的查询:
The keyword is not
if
, it iscase
, and you must useend
to end thecase
statement.Here is a query that can suit you:
虽然计数时的情况没有任何问题,但我只是想看看是否还有其他方法。
这利用了
,这种方法,正如上面显而易见的那样,不能“雄辩地”很好地总结所有空列。只是想看看如果没有 CASE 逻辑这是否可行。
While there is nothing wrong with the case WHEN counting, I just wanted to see if there was another way.
this utilizes the
This method, as is obvious above, does not 'eloquently' sum all null columns well. Just wanted to see if this was possible without the CASE logic.