[英]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.