1
+------+
| num  |
+------+
|    1 |
|    2 |
|    3 |
| NULL |
|    5 |
|    6 |
+------+

这是表格测试。

select max(num) from test;   ---> 6

如何编写如果存在 NULL 值的 SQL 语句 --> 输出应该是“OK”

如果此列中不存在 NULL 值 --> 输出应为 6

我尝试了很多次,但没有运气。有什么帮助吗?如果解决方案是 ANSI SQL 就好了

4

7 回答 7

3

试试这个:http ://www.sqlfiddle.com/#!2/dfcbd/2

这:

CREATE TABLE tbl
    (`num` int);

INSERT INTO tbl
    (`num`)
VALUES
    (1),
    (2),
    (3),
    (NULL),
    (5),
    (6);

select if(bit_or(num is null), 'OK', max(num)) as max from tbl;

输出:

OK

这:

CREATE TABLE tblx
    (`num` int);

INSERT INTO tblx
    (`num`)
VALUES
    (1),
    (2),
    (3),    
    (5),
    (6);

select if(bit_or(num is null), 'OK', max(num)) as max from tblx;

输出:

6
于 2012-06-14T06:26:04.450 回答
2

ANSI SQL 下面http://www.sqlfiddle.com/#!1/679b2/1

大多数数据库没有一流的布尔类型,因此查询更长。和许多数据库一样,不要将整数隐式转换为 varchar

select

  case when count(case when num is null then 1 end) > 0 then 
     'OK' 
  else
     cast(max(num) as varchar(16))
  end

from tbl

如果每个数据库都有一流的布尔值,则上面的内容可以缩短为:

select

  case when sum(cast( (num is null) as int )) > 0 then 
    'OK' 
  else
    cast(max(num) as varchar(16))
  end

from tbl

虽然如果每个数据库上真的有一个一流的布尔值,他们也会制作 bool_or/bit_or ,而且它更短:http ://www.sqlfiddle.com/#!1/679b2/6

select

  case when bool_or( num is null )  then 
    'OK' 
  else
    cast(max(num) as varchar(16))
  end

from tbl;
于 2012-06-14T06:57:54.120 回答
1

由于COUNT(column)折扣空值,您可以使用:

SELECT CASE WHEN COUNT(*) = COUNT(num) THEN MAX(num) ELSE "OK" END
  FROM Test;

我使用的 DBMS(Informix)要求 THEN 和 ELSE 中的类型相同,所以我最终测试了:

SELECT CASE WHEN COUNT(*) = COUNT(num) THEN CAST(MAX(num) AS CHAR(10)) ELSE "OK" END
  FROM Test;

它是一种冗长的强制转换方式(但它是标准 SQL);在给定的 DBMS 中通常有可用的速记。

示例输出:

+ CREATE TABLE Test(Num INTEGER);
+ INSERT INTO Test VALUES(1);
+ INSERT INTO Test VALUES(2);
+ INSERT INTO Test VALUES(3);
+ INSERT INTO Test VALUES(NULL);
+ INSERT INTO Test VALUES(5);
+ INSERT INTO Test VALUES(6);
+ SELECT CASE WHEN COUNT(*) = COUNT(num) THEN CAST(MAX(num) AS CHAR(10)) ELSE "OK" END
  FROM Test;
OK
+ DELETE FROM Test WHERE Num IS NULL;
+ SELECT CASE WHEN COUNT(*) = COUNT(num) THEN CAST(MAX(num) AS CHAR(10)) ELSE "OK" END
  FROM Test;
6
于 2012-06-14T06:18:24.970 回答
0

在我的 sql

Select (Case When hasNull > 0 Then 'OK' Else mx End) As Result
From
(
  Select Max(num) as mx,Sum(Case When numIs Null Then 1 Else 0 End) as hasNull 
  From test 
) as t
于 2012-06-14T06:10:55.933 回答
0

试试这个..

   select CASE num when num is null then "OK" else max(num) END from test; 
于 2012-06-14T06:12:14.903 回答
0

试试这个

select 
    case when ( select COUNT(*) from tableName where num is null ) > 0 then 'OK'
    else CONVERT(varchar, MAX(num) ) end as [num]
from tableName
于 2012-06-14T06:17:12.577 回答
0

如果您想避免运行两个查询(一个用于测试NULL值,然后一个用于计算max,您可以使用count(*)count(num)计算有值和没有NULL值的值的数量,如下所示(SQL Server 语法):

with TestQ as
(
   select max(num) as MaxNum, count(num) as NumCountNotNull, count(*) as NumCountTotal from nullAggTest
)
select 
   SpecialMax = case
      when NumCountNotNull < NumCountTotal then -1
      else MaxNum
   end
from TestQ

请注意,为简单起见,我将您的特殊值OK替换-1为,否则您需要添加一些内容,这并不重要:-)

于 2012-06-14T06:25:19.453 回答