表中空列和非空列的大小写表达式

时间:2020-04-27 08:52:44

标签: sql sql-server

我的桌子:

CREATE TABLE StudentScore (
    Student_ID INT,
    Student_Name NVARCHAR (50),
    Student_Score INT) 
GO

INSERT INTO StudentScore VALUES (1,'Ali', NULL)
INSERT INTO StudentScore VALUES (2,'Zaid', 770)
INSERT INTO StudentScore VALUES (3,'Mohd', 1140)
INSERT INTO StudentScore VALUES (4,NULL, 770)
INSERT INTO StudentScore VALUES (5,'John', 1240)
INSERT INTO StudentScore VALUES (6,'Mike', 1140)
INSERT INTO StudentScore VALUES (7,'Goerge', NULL)

查询已尝试

select * from StudentScore
Select TYPE1 =
CASE WHEN ANY(SELECT COLUMN IS NULL) THEN 'AT least 1 NULL'
    ELSE 'NON-NULL'
END

基本上,我希望如果表StudentScore中的任何列都具有单个null bvalue,则该列的类型应为null,否则不应为null(请注意,这是面试问题的一部分,我不能使用{ {1}}等。我需要使用用例。任何人都可以帮忙

例如这里的ID将为NON-NULL,其余两个将为“至少1个null”类型

在看到答案以澄清之后进行编辑:

我希望我的代码应该检查列的所有行,如果列中的所有行都不为null,则返回'Non-null'。 所有列均应单独检查。

例如该代码给出的输出如下:

information_schema

以上不是我想要的输出

我想要的输出是

列不为空:ID ,至少1个空值(在对应于一列的所有行中):Student_Score,Student_name。

因此,如果对于特定列,所有行中至少存在一个空值,则代码应返回“至少1个空值”

例如它应检查与每一列相对应的所有8行,并且如果与一列相对应的所有行中都没有空值,则只有该列将变为“不为空”

我还删除了主键以使问题更通用。

3 个答案:

答案 0 :(得分:2)

在这种情况下,您不能使用所有列运算符(*)
您可以尝试对每种显式命名列

使用OR条件的情况
    select case when col1 is null 
            OR col2 is null 
            OR  col3 is NULL then 'AT least 1 NULL'
            ELSE 'NON-NULL' END type
    from StudentScore

以您的情况

    select Student_ID, Student_Name, Student_Score
    , case when Student_Name is null 
                OR Student_Score is null 
                then 'AT least 1 NULL'
                ELSE 'NON-NULL' END type
        from StudentScore

答案 1 :(得分:2)

我想您想要1行和1列作为结果,因此可以使用EXISTS:

select 
  case 
    when exists (select 1 from StudentScore where Student_Name is null) then 'AT least 1 NULL'
    when exists (select 1 from StudentScore where Student_Score is null) then 'AT least 1 NULL'
    else 'NON-NULL'
  end TYPE1 

或:

select 
  case 
    when exists (select 1 from StudentScore where Student_Name is null)
    or exists (select 1 from StudentScore where Student_Score is null) then 'AT least 1 NULL'
    else 'NON-NULL'
  end TYPE1

请参见demo
结果:

> | TYPE1           |
> | :-------------- |
> | AT least 1 NULL |

如果您希望每个列有1个结果:

select 
  case when exists (select 1 from StudentScore where Student_ID is null) then 'AT least 1 NULL' else 'NON-NULL' end ID,
  case when exists (select 1 from StudentScore where Student_Name is null) then 'AT least 1 NULL' else 'NON-NULL' end Student_Name,
  case  when exists (select 1 from StudentScore where Student_Score is null) then 'AT least 1 NULL' else 'NON-NULL' end Student_Score

请参见demo
结果:

> ID       | Student_Name    | Student_Score  
> :------- | :-------------- | :--------------
> NON-NULL | AT least 1 NULL | AT least 1 NULL

或者如果您想要2行,每种类型1行,列名作为逗号分隔列表:

select type, string_agg(colname, ',') columns
from (
  select 'id' colname, case when exists (select 1 from StudentScore where Student_ID is null) then 'AT least 1 NULL' else 'NON-NULL' end type
  union all
  select 'Student_Name', case when exists (select 1 from StudentScore where Student_Name is null) then 'AT least 1 NULL' else 'NON-NULL' end
  union all
  select 'Student_Score', case when exists (select 1 from StudentScore where Student_Score is null) then 'AT least 1 NULL' else 'NON-NULL' end
) t
group by type

此代码适用于SQL Server 2017+。
请参见demo
结果:

> type            | columns                   
> :-------------- | :-------------------------
> AT least 1 NULL | Student_Name,Student_Score
> NON-NULL        | id   

答案 2 :(得分:1)

最简单的方法是将其放在单独的列中:

select (case when count(*) = count(student_id) then 'No Null Values' else 'Null Values' end) as student_id,
       (case when count(*) = count(student_name) then 'No Null Values' else 'Null Values' end) as student_name,
       (case when count(*) = count(student_score) then 'No Null Values' else 'Null Values' end) as student_score
from studentscores;

这应该是您要做的最简单,最高效的方法。

如果要在单独的行中显示,则只需取消以下结果即可:

select v.*
from (select (case when count(*) = count(student_id) then 'No Null Values' else 'Null Values' end) as student_id,
             (case when count(*) = count(student_name) then 'No Null Values' else 'Null Values' end) as student_name,
             (case when count(*) = count(student_score) then 'No Null Values' else 'Null Values' end) as student_score
      from studentscores
     ) ss cross apply
     (values ('student_id', student_id), ('student_name', student_name), ('student_score', student_score)
     ) v(col, str)
相关问题