Sql server 获取所有列中具有空值的行

Sql server 获取所有列中具有空值的行,sql-server,Sql Server,例如,我有一个返回以下结果的查询 ID Name Year Age 0 NULL 2013 23 1 Luis NULL 24 2 Jose 2010 NULL 3 Fernando 2003 43 如果某些列(在本例中为Name、Year、Age)至少有一行具有空值,否则为0行,则我希望获取所有行。例如,在公开的示例中,我得到4行,因为每列至少有一个空值 另一方面: ID Name Year Ag

例如,我有一个返回以下结果的查询

ID   Name      Year  Age
0    NULL      2013  23
1    Luis      NULL  24
2    Jose      2010  NULL
3    Fernando  2003  43
如果某些列(在本例中为Name、Year、Age)至少有一行具有空值,否则为0行,则我希望获取所有行。例如,在公开的示例中,我得到4行,因为每列至少有一个空值

另一方面:

ID   Name      Year  Age
0    NULL      2013  23
1    Luis      NULL  24
2    Jose      2010  34 
3    Fernando  2003  43
年龄没有空值,因此我得到0行

提前谢谢

使用以下命令:

  with cte as (
    select case when count(*)=count(year) or count(*) = count(name) or count(*)=count(age) then 0 else 1 end as val from data
    )
    select data.* from data,cte where 1 = cte.val
使用以下命令:

  with cte as (
    select case when count(*)=count(year) or count(*) = count(name) or count(*)=count(age) then 0 else 1 end as val from data
    )
    select data.* from data,cte where 1 = cte.val

@mrida的版本非常完美,但我使用了CTE,只进行计数计算,以便于支持

/* test tables:
create table t1 (ID int,Name varchar(100),[Year] int, Age int)

insert t1
select 0,NULL,2013,23 union all
select 1,'Luis',NULL,24 union all
select 2,'Jose',2010,NULL union all
select 3,'Fernando',2003,43

create table t2 (ID int,Name varchar(100),[Year] int, Age int)

insert t2
select 0,NULL,2013,23 union all
select 1,'Luis',NULL,24 union all
select 2,'Jose',2010,34 union all
select 3,'Fernando',2003,43
*/

--for Jose with undefined age
with cte as (select count(*) as AllCount,count(year) as YearsCount,count(name) as NamesCount,count(age) as AgesCount from t1)
select t1.* from t1,cte
where not (cte.AllCount=cte.YearsCount or cte.AllCount=cte.NamesCount or cte.AllCount=cte.AgesCount)

--for 34-aged Jose :)
with cte as (select count(*) as AllCount,count(year) as YearsCount,count(name) as NamesCount,count(age) as AgesCount from t2)
select t2.* from t2,cte
where not (cte.AllCount=cte.YearsCount or cte.AllCount=cte.NamesCount or cte.AllCount=cte.AgesCount)

@mrida的版本非常完美,但我使用了CTE,只进行计数计算,以便于支持

/* test tables:
create table t1 (ID int,Name varchar(100),[Year] int, Age int)

insert t1
select 0,NULL,2013,23 union all
select 1,'Luis',NULL,24 union all
select 2,'Jose',2010,NULL union all
select 3,'Fernando',2003,43

create table t2 (ID int,Name varchar(100),[Year] int, Age int)

insert t2
select 0,NULL,2013,23 union all
select 1,'Luis',NULL,24 union all
select 2,'Jose',2010,34 union all
select 3,'Fernando',2003,43
*/

--for Jose with undefined age
with cte as (select count(*) as AllCount,count(year) as YearsCount,count(name) as NamesCount,count(age) as AgesCount from t1)
select t1.* from t1,cte
where not (cte.AllCount=cte.YearsCount or cte.AllCount=cte.NamesCount or cte.AllCount=cte.AgesCount)

--for 34-aged Jose :)
with cte as (select count(*) as AllCount,count(year) as YearsCount,count(name) as NamesCount,count(age) as AgesCount from t2)
select t2.* from t2,cte
where not (cte.AllCount=cte.YearsCount or cte.AllCount=cte.NamesCount or cte.AllCount=cte.AgesCount)