Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/25.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.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_Sql Server 2012 - Fatal编程技术网

所有Where子句项的SQL结果

所有Where子句项的SQL结果,sql,sql-server,sql-server-2012,Sql,Sql Server,Sql Server 2012,在SQL Server 2012中,我需要查询为where子句中的每个元素返回一个值 查询返回 idCount Squad 1 Civil 3 Electrical 3 Mechanical 我希望输出是 idCount Squad 0 Architectural 1 Civil 3 Electrical 3 Mechanical 您需要一个左连接。在SQL Serve

在SQL Server 2012中,我需要查询为where子句中的每个元素返回一个值

查询返回

idCount Squad
1       Civil
3       Electrical
3       Mechanical
我希望输出是

idCount     Squad
    0       Architectural
    1       Civil
    3       Electrical
    3       Mechanical

您需要一个
左连接
。在SQL Server中,您可以执行以下操作:

select count(p.id), v.squad
from (values ('Mechanical'), ('Civil'), ('Electrical'), ('Architectural')
     ) v(squad) left join
     dbo.Project p
     on v.squad = p.squad and
        p.id between 60000 and 80000 and
        p.fiscalyear = 2018
group by v.squad
order by v.squad asc ;

请注意,所有的
where
条件都需要放在
on
子句中,以防止无意中过滤不匹配的值。

您需要一个
左连接。在SQL Server中,您可以执行以下操作:

select count(p.id), v.squad
from (values ('Mechanical'), ('Civil'), ('Electrical'), ('Architectural')
     ) v(squad) left join
     dbo.Project p
     on v.squad = p.squad and
        p.id between 60000 and 80000 and
        p.fiscalyear = 2018
group by v.squad
order by v.squad asc ;
请注意,所有的
where
条件都需要放在
on
子句中,以防止无意中过滤不匹配的值