首页 > 解决方案 > 有没有办法让 max 函数忽略 sqlite 列中的空值?

问题描述

简而言之,如果我有一个包含两列(例如人员和高度)的表格,我可以使用 select max(height) from 'table' group by people,它将为任何一组人员生成最大高度_忽略任何空值_。

但是,如果我有一个包含更多列(height_1、height_2、height_3 等)的表,并且我想用一个显示最大高度(height_1、height_2、height_3、max_height)的新列来简单地重现该表,我会直观地喜欢放。

select
  height_1,
  height_2,
  height_3,
  max(height_1, height_2, height_3) as max_height
from 'table'

但是以这种方式使用,max 似乎表现不同,如果任何参数为 null,则返回 null。

是否有某种形式的使用可以让它在聚合列时表现得像它一样,并忽略空值?即使在一系列列中聚合单个值?

标签: sqlite

解决方案


你可以使用类似的东西:

SELECT height_1
     , height_2
     , height_3
     , max(ifnull(height_1, 0), ifnull(height_2, 0), ifnull(height_3, 0)) AS max_height
FROM "table"

推荐阅读