Select 行,如果它只包含一个值
Select rows if it contains only one value
我正在尝试查找 Col J 到 Col L 中所有为空的行
我的输出应该如下所示(绿色是我期望的,其他的应该被忽略):
我试过下面的代码,但没有用:
-- Step 1 get all the data as required
select Col A, Col B, Col C, Col D, Col E, Col F, Col G, Col H, Col I, Col J, Col K, Col L,
into tempDB
from MainDB
-- Step 2
select ColB, min(Col C) , min(Col D), min(Col E), min(Col F)
from tempDB
where Col J = Col K and Col L = null
group by Col B
您需要使用 IS NULL
而不是 = NULL
。您无法比较 null 是否等于某物,只要它 是 null 即可。
SELECT [ColB], MIN([Col C]), MIN([Col D]), MIN([Col E]), MIN([Col F])
FROM tempDB
WHERE [Col J] IS NULL
AND [Col K] IS NULL
AND [Col L] IS NULL
GROUP BY [Col B]
我对你的要求的理解是正确的,你想要 ColJ
、ColK
、ColL
中所有值都是 NULL 值的行
select ColB, min(ColC) , min(ColD), min(ColE), min(ColF)
from tempDB
group by ColB
having min(ColJ) is null
and min(ColK) is null
and min(ColL) is null
这应该有效。
SELECT *
FROM tempdb
where colj is null and colk is null and coll is null
你不应该使用 something=null 条件因为
null 不是什么都不是
null 是未定义的东西。
进一步阅读
https://www.w3schools.com/sql/sql_null_values.asp
编辑:- 如果您想要基于行的 colb 值
SELECT *
FROM tempdb
where colj is null and colk is null and coll is null and colb=4444
如果数据类型不是数字(char、varchar 等),则在 4444 左右输入 ''。
如果您希望 colb
colj
、colk
和 coll
中的所有行都包含空值:
select ColB, min(ColJ), min(ColK), min(ColL)
from tempDB
group by ColB
having coalesce(min(ColJ), min(ColK), min(ColL)) is null
试试这个-
SELECT ColB,
MIN(ColC) , MIN(ColD), MIN(ColE), MIN(ColF)
FROM tempDB
GROUP BY Col B
HEVING SUM(COALESCE (ColJ,0)) = 0
AND SUM(COALESCE (ColK,0)) = 0
AND SUM(COALESCE (ColL,0)) = 0
SELECT
t.[Col A],
t.[Col B],
t.[Col C],
t.[Col D],
t.[Col E],
t.[Col F],
t.[Col G],
t.[Col H],
t.[Col J],
t.[Col K],
t.[Col L]
FROM tempDB t
INNER JOIN
(
SELECT MIN([Col B]) AS [Col B]
FROM tempDB
GROUP BY
[Col B]
HAVING
MIN([Col J]) IS NULL
AND MIN([Col K]) IS NULL
AND MIN([Col L]) IS NULL
) d
on t.[Col B] = d.[Col B]
我正在尝试查找 Col J 到 Col L 中所有为空的行
我的输出应该如下所示(绿色是我期望的,其他的应该被忽略):
-- Step 1 get all the data as required
select Col A, Col B, Col C, Col D, Col E, Col F, Col G, Col H, Col I, Col J, Col K, Col L,
into tempDB
from MainDB
-- Step 2
select ColB, min(Col C) , min(Col D), min(Col E), min(Col F)
from tempDB
where Col J = Col K and Col L = null
group by Col B
您需要使用 IS NULL
而不是 = NULL
。您无法比较 null 是否等于某物,只要它 是 null 即可。
SELECT [ColB], MIN([Col C]), MIN([Col D]), MIN([Col E]), MIN([Col F])
FROM tempDB
WHERE [Col J] IS NULL
AND [Col K] IS NULL
AND [Col L] IS NULL
GROUP BY [Col B]
我对你的要求的理解是正确的,你想要 ColJ
、ColK
、ColL
中所有值都是 NULL 值的行
select ColB, min(ColC) , min(ColD), min(ColE), min(ColF)
from tempDB
group by ColB
having min(ColJ) is null
and min(ColK) is null
and min(ColL) is null
这应该有效。
SELECT *
FROM tempdb
where colj is null and colk is null and coll is null
你不应该使用 something=null 条件因为 null 不是什么都不是 null 是未定义的东西。
进一步阅读 https://www.w3schools.com/sql/sql_null_values.asp
编辑:- 如果您想要基于行的 colb 值
SELECT *
FROM tempdb
where colj is null and colk is null and coll is null and colb=4444
如果数据类型不是数字(char、varchar 等),则在 4444 左右输入 ''。
如果您希望 colb
colj
、colk
和 coll
中的所有行都包含空值:
select ColB, min(ColJ), min(ColK), min(ColL)
from tempDB
group by ColB
having coalesce(min(ColJ), min(ColK), min(ColL)) is null
试试这个-
SELECT ColB,
MIN(ColC) , MIN(ColD), MIN(ColE), MIN(ColF)
FROM tempDB
GROUP BY Col B
HEVING SUM(COALESCE (ColJ,0)) = 0
AND SUM(COALESCE (ColK,0)) = 0
AND SUM(COALESCE (ColL,0)) = 0
SELECT
t.[Col A],
t.[Col B],
t.[Col C],
t.[Col D],
t.[Col E],
t.[Col F],
t.[Col G],
t.[Col H],
t.[Col J],
t.[Col K],
t.[Col L]
FROM tempDB t
INNER JOIN
(
SELECT MIN([Col B]) AS [Col B]
FROM tempDB
GROUP BY
[Col B]
HAVING
MIN([Col J]) IS NULL
AND MIN([Col K]) IS NULL
AND MIN([Col L]) IS NULL
) d
on t.[Col B] = d.[Col B]