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
在SQL中正确使用Count()和Sum()?_Sql_Sql Server_Tsql_Count_Sum - Fatal编程技术网

在SQL中正确使用Count()和Sum()?

在SQL中正确使用Count()和Sum()?,sql,sql-server,tsql,count,sum,Sql,Sql Server,Tsql,Count,Sum,好的,我希望我能很好地解释这个问题,因为我觉得这将是一个艰难的问题 我今天有两张桌子。这些看起来像: @pset table (PersonID int, SystemID int, EntitlementID int, TargetID int) @Connector table (TargetName varchar(10), fConnector bit) 第一个表存储的记录告诉我,哦,这个人有这个系统,它由这些权利组成,谁有这些目标。有点复杂,但请跟我来。第二个存储TargetNam

好的,我希望我能很好地解释这个问题,因为我觉得这将是一个艰难的问题

我今天有两张桌子。这些看起来像:

@pset table (PersonID int, SystemID int, EntitlementID int, TargetID int)

@Connector table (TargetName varchar(10), fConnector bit)
第一个表存储的记录告诉我,哦,这个人有这个系统,它由这些权利组成,谁有这些目标。有点复杂,但请跟我来。第二个存储TargetName,然后在我不太理论的系统中该目标是否有连接器

我要做的是合并这两个表,这样我就可以看到@pset中每一行的目标标志。这将帮助我以后,你会看到

如果系统中的每个授权都有一个到目标的连接器,那么该标志对所有授权都是真的,那么我想知道

其他人都应该换一张桌子

这是我试图做的,但没有成功。我需要知道我哪里出错了。希望有比我更有经验的人能够回答

-- If the count(123) = 10 (ten rows with SystemID = 123) and the sum = 10, cool.
select pset.*, conn.fConnector from @pset pset
inner join vuTargets vt
on vt.TargetID = pset.TargetID
inner join @conn conn
on  conn.TargetName = vt.TargetName
group by ProfileID, SystemRoleID, EntitlementID, TargetID, fConnector
having count(SystemID) = sum(cast(fConnector as int))
order by ProfileID

不幸的是,这些措施不起作用:

编辑


下面是显示问题的屏幕截图。请注意ProfileID1599的系统ID为1126567,但其中一项权利没有连接器!如何将这两行都放入第二个查询中?上图

您的基本问题是,您试图将两个不同的记录集汇总到一起。 SELECT和GROUP BY子句的初始集合表示,对于集合[ProfileId、SystemId、AuthenticationId、TargetId、fConnector]中的每个差异,都需要一条记录。 HAVING子句的第二个集合表示,对于初始集合中的每一行,您希望将其记录计数与连接总数进行比较。但是,由于您要求分组到单个标志,因此假设1对1的关系,这会产生为每个标志获取一行的效果。实际上,你在说-'嘿,如果这个目标有关联?是的,我想要它

您似乎想要的是向上滚动到SystemId值。为此,您需要将SELECT和GROUP BY子句更改为仅包含集合[ProfileId,SystemId]。这将仅返回从配置文件和系统键入的所有目标为“已连接”的行。您将无法看到个人权利、目标以及它们是否相互关联。但是,您将能够推断出它们都将相互关联

编辑:

为了充分披露,以下是您如何获得类似于原始结果集的内容,其中列出了所有授权ID和targetID:

这将为您提供ProfileId/SystemRoleId键的列表,其中所有的AuthenticationID和TargetId都有连接,或者,将CTE=翻转到并非所有都有连接的位置。

编辑:修复了我的原始查询,还更新了描述

您可以将其拆分:首先查找fConnector为0的targetID。然后找到PersonID和SystemID对,它们的任何目标都与您找到的目标相同。然后选择相关数据:这将查找PersonID、SystemID对,其中至少有一个授权没有到目标的连接器

with abc as (
    select PersonID, SystemID
    from pset P
    where TargetID in (
        select TargetID
        from vuTargets V join connector C on V.TargetName = C.TargetName
        where C.fConnector = 0
        )
)
select P.PersonID, P.SystemID, P.EntitlementID, P.TargetID, C.fConnector
from pset P
    join abc on ((P.PersonID = abc.PersonID) and (P.SystemID = abc.SystemID))
    join vuTargets V on P.TargetID = V.TargetID
    join connector C on V.TargetName = C.TargetName
查找PersonID和SystemID对的查询类似,其中所有权利都有到目标的连接器:

with abc as (
    select PersonID, SystemID
    from pset P
    where TargetID in (
        select TargetID
        from vuTargets V join connector C on V.TargetName = C.TargetName
        where C.fConnector = 0
        )
)
select P.PersonID, P.SystemID, P.EntitlementID, P.TargetID, C.fConnector
from 
    pset P
    join abc on ((P.PersonID <> abc.PersonID) or (P.SystemID <> abc.SystemID))
    join vuTargets V on P.TargetID = V.TargetID
    join connector C on V.TargetName = C.TargetName

区别在于与临时表vs=的联接。这与zero的答案非常相似,但不使用计数或总和。

您在查询中使用的列不在提供的架构中,例如TargetObjectID。请发布正确的架构。抱歉,我试图清理查询,但没有清理,现在应该可以了。因此,您想查找PersonId和SystemId,其中每个授权都有一个fConnector等于1的所有目标?是的,您比我解释得更好。我会尝试一下,希望能奏效@TommyFisk-我添加了一个查询,该查询将为您提供与结果集类似的显示结果,但仅当您的原始条件匹配时才显示。-1但这实际上会让他获得原始结果。在外部选择中连接到的表也没有使用。@X-Zero我看到问题了。编辑查询以修复它。也更新了答案。谢谢,-1删除。虽然。。。。。。或感觉违反直觉。我更可能尝试将CTE更改为使用NOT IN而不是IN,或者对其使用异常联接。不过有点个人偏好。
with abc as (
    select PersonID, SystemID
    from pset P
    where TargetID in (
        select TargetID
        from vuTargets V join connector C on V.TargetName = C.TargetName
        where C.fConnector = 0
        )
)
select P.PersonID, P.SystemID, P.EntitlementID, P.TargetID, C.fConnector
from pset P
    join abc on ((P.PersonID = abc.PersonID) and (P.SystemID = abc.SystemID))
    join vuTargets V on P.TargetID = V.TargetID
    join connector C on V.TargetName = C.TargetName
with abc as (
    select PersonID, SystemID
    from pset P
    where TargetID in (
        select TargetID
        from vuTargets V join connector C on V.TargetName = C.TargetName
        where C.fConnector = 0
        )
)
select P.PersonID, P.SystemID, P.EntitlementID, P.TargetID, C.fConnector
from 
    pset P
    join abc on ((P.PersonID <> abc.PersonID) or (P.SystemID <> abc.SystemID))
    join vuTargets V on P.TargetID = V.TargetID
    join connector C on V.TargetName = C.TargetName