简体   繁体   English

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

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

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

Query tried查询已尝试

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

Basically I want that if there is any single null bvalue for any column in table StudentScore, then the type of that column should be null else it should be not null (note that this is part of interview question and I cannot use information_schema etc. I need to do this using case. Can anyone help基本上我希望,如果表 StudentScore 中的任何列都有任何单个 null bvalue,那么该列的类型应该是 null 否则它不应该是 null 不能使用information_schema ,这部分是。需要做这个用例。任何人都可以帮忙

For eg here ID will be NON-NULL, rest two will be type 'At least 1 null'例如,这里的 ID 将是 NON-NULL,rest 两个将是类型“至少 1 个空”

EDIT after seeing answers to clarify:看到答案后进行编辑以澄清:

I want that my code should check all rows of columns and return 'Non-null' if all the rows in a column are not null.如果列中的所有行都不是 null,我希望我的代码应该检查所有列行并返回“非空”。 All the columns should be checked individually.应单独检查所有列。

For eg this code gives output as below:例如,此代码给出 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

The above is not my desired output .以上不是我想要的 output

My desired output is我想要的 output 是

Not null columns : ID, At least 1 null value (in all the rows corresponding to one column): Student_Score, Student_name.不是 null 列:ID,至少 1 个 null 值(在与一列对应的所有行中):Student_Score,Student_name。

So the code should return 'at least 1 null value' if for a particular column, there is at least one null value present in all the rows .因此,如果对于特定列,代码应返回“至少 1 个 null 值”,所有行中至少存在一个 null 值

For eg it should check all 8 rows corresponding to each column and if there is no null value in all rows corresponding to one column, then only that column is going to 'Not null'例如,它应该检查与每一列对应的所有 8 行,如果在与一列对应的所有行中没有 null 值,则只有该列将变为“非空”

Also I removed primary key to make question more generic.我还删除了主键以使问题更通用。

You can't use the all columns operator (*) in this case在这种情况下,您不能使用所有列运算符 (*)
you could try using a case when with OR condition for each explicit named column您可以尝试对每个显式命名列使用 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

in your case在你的情况下

    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

I guess you want 1 row and 1 column as result, so you can use EXISTS:我猜你想要 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 

or:或者:

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

See the demo .演示
Result:结果:

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

If you want 1 result for each of the columns:如果您希望每列有 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

See the demo .演示
Results:结果:

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

Or if you want 2 rows, 1 for each type and the column names as a comma separated list:或者,如果您想要 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

This code works for SQL Server 2017+.此代码适用于 SQL Server 2017+。
See the demo .演示
Results:结果:

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

The simplest method is to put this into separate columns:最简单的方法是将其放入单独的列中:

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;

This should be the simplest and most performance way to do what you want.这应该是做你想做的最简单和最高效的方法。

If you want this in separate rows, I would just unpivot these results:如果您希望将其放在单独的行中,我将取消透视这些结果:

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