Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在where子句中使用查询别名的sql_Sql_Sql Server - Fatal编程技术网

在where子句中使用查询别名的sql

在where子句中使用查询别名的sql,sql,sql-server,Sql,Sql Server,我试图从ManagementStudio(MSSQL)中的查询中获得结果。如何在where not exists子句中使用查询结果,如下所示: select res.id from (select id, column1 from table1) as res where not exists (select id from res where column1 is null) 查询更复杂,但我想知道如何在where子句中使用别名为的查询,其中使用not exists 当您说您的查询更复杂时,

我试图从ManagementStudio(MSSQL)中的查询中获得结果。如何在where not exists子句中使用查询结果,如下所示:

select res.id
from
(select id, column1 from table1) as res
where not exists (select id from res where column1 is null)

查询更复杂,但我想知道如何在where子句中使用别名为的查询,其中使用not exists

当您说您的查询更复杂时,我假设有很多事情,因为提供的简化场景没有充分的意义,但是您本质上寻找的功能是CTEs(),因为它们可以以这种方式使用

e、 g


本质上,CTE是查询的命名别名,您可以使用该别名引用该别名,而不是每次重复整个查询

当您说您的查询更复杂时,我假设有很多事情,因为提供的简化场景没有充分的意义,但是您本质上寻找的功能是CTEs(),因为它们可以以这种方式使用

e、 g


本质上,CTE是查询的命名别名,您可以使用该别名引用该别名,而不是每次重复整个查询

注意:您可以使用
来表达此逻辑,但
除外:

select t1.id
from table1 t1
except
select t1.id
from table1 t1
where column1 is null;
如果
table1
确实是一个复杂的查询,那么此方法和CTE方法都需要对其进行两次评估。相反,您可以使用窗口功能:

select id
from (select t1.*,
             sum(case when column1 is null then 1 else 0 end) over (partition by id) as cnt_column1_null
      from table1
     ) t1
where cnt_column1_null > 0;
或聚合:

select t1.id
from table1 t1
group by t1.id
having sum(case when column1 is null then 1 else 0 end) = 0;

注意:您可以使用
来表达此逻辑,但
除外:

select t1.id
from table1 t1
except
select t1.id
from table1 t1
where column1 is null;
如果
table1
确实是一个复杂的查询,那么此方法和CTE方法都需要对其进行两次评估。相反,您可以使用窗口功能:

select id
from (select t1.*,
             sum(case when column1 is null then 1 else 0 end) over (partition by id) as cnt_column1_null
      from table1
     ) t1
where cnt_column1_null > 0;
或聚合:

select t1.id
from table1 t1
group by t1.id
having sum(case when column1 is null then 1 else 0 end) = 0;

你不能。考虑CTE代替,你不能。考虑一下CTE。非常感谢,我使用了聚合查询,这对我来说很有用。非常感谢,我使用了聚合查询,它对我来说很有用。