Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.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/8/visual-studio-code/3.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
Sql server 2008 SQL SERVER-查找具有相同特征的组成员_Sql Server 2008 - Fatal编程技术网

Sql server 2008 SQL SERVER-查找具有相同特征的组成员

Sql server 2008 SQL SERVER-查找具有相同特征的组成员,sql-server-2008,Sql Server 2008,我的数据代理是组的一部分。每个代理可以在多个州获得许可。我正在尝试执行一个查询,该查询采用客户机状态,并检查组中的每个代理是否在客户机状态下获得许可 以下是示例数据: CREATE TABLE GroupAgentState ( GroupID int, AgentID int, StateCd CHAR(2) ) INSERT INTO GroupAgentState VALUES (1,100, 'OH'), (1, 100, 'NH'), (1,100,'NY'), (1, 101,

我的数据代理是组的一部分。每个代理可以在多个州获得许可。我正在尝试执行一个查询,该查询采用客户机状态,并检查组中的每个代理是否在客户机状态下获得许可

以下是示例数据:

CREATE TABLE GroupAgentState (
GroupID int,
AgentID int,
StateCd CHAR(2) )

INSERT INTO GroupAgentState VALUES
(1,100, 'OH'), 
(1, 100, 'NH'),
(1,100,'NY'),
(1, 101, 'OH'),
(1, 101, 'NY'),
(1, 102, 'NY')
我想做一个检查,这样如果我有一个Client State@ClientState,并且该Client与一个组有关系,那么组中的所有代理是否都在Client State中获得许可

对于我的示例数据,我希望如果客户机A与组1有关系并且@ClientState='OH',那么返回值将为false。如果@ClientState='NY',则返回值为true

我在这件事上有点不知所措


提前谢谢

此查询显示未在客户端状态下注册的所有代理:

SELECT AgentID
FROM GroupAgentState gas1
WHERE GroupID =  @testGroup
  AND StateCd <> @ClientState
  AND NOT EXISTS( Select *
                  From GroupAgentState gas2
                  Where gas2.StateCd = @ClientState
                    And gas2.GroupID = gas1.GroupID
                    And gas2.AgentID = gas1.AgentID
                 )

这可能是一个好的开始:

-- returns count of agents unlicensed in a particular state (@ClientState) by group
select
    gas.GroupId
    , sum( case when gas.AgentId is null then 1 else 0 end ) UnlicensedAgents
from
    Agent a
    left outer join GroupAgentState gas
     on a.AgentId = gas.AgentId
        and gas.StateCd = @ClientState
group by
    gas.GroupId

到目前为止你尝试了什么?谢谢莫霍-我正想走计数的路线,但我希望有一个更迷人的解决方案。。。