Sql server 查找EVA设计中缺少的属性数量。。。。(从此sql联接中删除子查询)
如何从以下示例中删除[Count of Missing Attributes]中的子查询Sql server 查找EVA设计中缺少的属性数量。。。。(从此sql联接中删除子查询),sql-server,Sql Server,如何从以下示例中删除[Count of Missing Attributes]中的子查询 --Setup data Declare @Attributes table (AttributeID int) Declare @Objects table (ObjectID int) Declare @ObjectAttributes table (ObjectID int, AttributeID int, val int) Insert Into @Obj
--Setup data
Declare @Attributes table (AttributeID int)
Declare @Objects table (ObjectID int)
Declare @ObjectAttributes table (ObjectID int, AttributeID int, val int)
Insert Into @Objects (ObjectID) values (1)
Insert Into @Objects (ObjectID) values (2)
Insert Into @Attributes (AttributeID) values (1)
Insert Into @Attributes (AttributeID) values (2)
Insert Into @ObjectAttributes (ObjectID,AttributeID) values (1,1)
Insert Into @ObjectAttributes (ObjectID,AttributeID) values (2,1)
Insert Into @ObjectAttributes (ObjectID,AttributeID) values (2,2)
--Query
Select
ObjectID,
(Select Count(Distinct a2.AttributeID) FROM @Attributes a2) As [Total Count Of Attributes],
Count(Distinct a.AttributeID) as [Count of attributes for this object],
(Select Count(Distinct a2.AttributeID) FROM @Attributes a2) - Count(Distinct a.AttributeID) as [Count of Missing Attributes]
FROM @Attributes a
INNER Join @ObjectAttributes oa
On oa.AttributeID = a.AttributeID
Group By ObjectID
Order By ObjectID
这假定不存在重复数据。删除HAVING子句以包含没有属性的对象(这些属性在原始查询中被忽略)
Arg-不要将“sqlserver”用作标记。在您提出此问题时,没有其他“sqlserver”问题和7000多个“sqlserver”问题。现在,由于您创建了“sqlserver”标记,我不得不在另外两个地方用抱歉的Joel清理它,我只是点击得太快了
SELECT
ob.Objectid
,count(at.Attributeid) [Total Count Of Attributes]
,count(oa.Attributeid) [Count of attributes for this object]
,sum(case when oa.AttributeId is null then 1 else 0 end) [Count of Missing Attributes]
from @Attributes at
cross join @Objects ob
left outer join @ObjectAttributes oa
on oa.ObjectId = ob.ObjectId
and oa.AttributeId = at.AttributeId
group by ob.Objectid
having count(distinct oa.Attributeid) > 0