繁体   English   中英

表中 null 而不是 null 列的案例表达式

[英]Case expression for null and not null columns in a table

我的桌子:

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 不能使用information_schema ,这部分是。需要做这个用例。任何人都可以帮忙

例如,这里的 ID 将是 NON-NULL,rest 两个将是类型“至少 1 个空”

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

如果列中的所有行都不是 null,我希望我的代码应该检查所有列行并返回“非空”。 应单独检查所有列。

例如,此代码给出 output 如下:

select case when Student_Score is null then 'non-null'
else 'non-null'
end TYPE1 from StudentScore

TYPE1
non-null
non-null
non-null
non-null
non-null
non-null
non-null

以上不是我想要的 output

我想要的 output 是

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

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

例如,它应该检查与每一列对应的所有 8 行,如果在与一列对应的所有行中没有 null 值,则只有该列将变为“非空”

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

在这种情况下,您不能使用所有列运算符 (*)
您可以尝试对每个显式命名列使用 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 行和 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

演示
结果:

> | 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

演示
结果:

> 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+。
演示
结果:

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

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

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM