SQL Server多计数和一个组

时间:2016-10-25 06:46:27

标签: sql sql-server

我有3个表:Systemuser,Activitypointer和new_TroubleticketTracking。 我想通过用户名来获取activitypointer的数量和troubletickettracking分组的数量。

我成功地将count和group by用于两个表。这是我的代码:

Select dbo.SystemUser.FullName as [Helpdesk],
       COUNT(dbo.ActivityPointer.OwnerIdName) AS [Activity Related] 
from   dbo.SystemUser 
INNER JOIN dbo.ActivityPointer 
        ON dbo.SystemUser.SystemUserId = dbo.ActivityPointer.OwnerId 
group by dbo.SystemUser.FullName

结果:

Helpdesk|  Activity Related
a       |        10
b       |         5

但是我无法为第3张表添加计数。我试过这样的事情:

Select dbo.SystemUser.FullName as [Helpdesk],
       COUNT(dbo.ActivityPointer.OwnerIdName) AS [Activity Related],
       COUNT(dbo.new_troubletickettracking.OwnerIdName) AS [Test]
from   dbo.SystemUser 
INNER JOIN dbo.ActivityPointer 
        ON dbo.SystemUser.SystemUserId = dbo.ActivityPointer.OwnerId 
INNER JOIN dbo.new_troubletickettracking 
        ON dbo.SystemUser.SystemUserId = dbo.new_troubletickettracking.OwnerId 
group by dbo.SystemUser.FullName

但结果是混乱。我希望结果看起来像:

Helpdesk|  Activity Related  | Troube Ticket
a       |        10          |     20  
b       |         5          |     15

4 个答案:

答案 0 :(得分:0)

结果是混乱 =之前数字的倍数?

两个表中的每个用户都有多行,您的联接会产生比预期更多的行。

您可以使用COUNT(DISTINCT ...)

Select dbo.SystemUser.FullName as [Helpdesk],
       COUNT(DISTINCT dbo.ActivityPointer.OwnerIdName) AS [Activity Related],
       COUNT(DISTINCT dbo.new_troubletickettracking.OwnerIdName) AS [Test]
from   dbo.SystemUser 
INNER JOIN dbo.ActivityPointer 
        ON dbo.SystemUser.SystemUserId = dbo.ActivityPointer.OwnerId 
INNER JOIN dbo.new_troubletickettracking 
        ON dbo.SystemUser.SystemUserId = dbo.new_troubletickettracking.OwnerId 
group by dbo.SystemUser.FullName

假设您的联接是PK-FK关系,您最好对派生表/公用表表达式中的各个表进行计数并加入结果。这将更有效:

Select su.FullName as [Helpdesk],
       ar.[Activity Related],
       tt.[Test]
from   dbo.SystemUser AS su
INNER JOIN
 (
   SELECT OwnerId,
      -- do you really need COUNT(OwnerIdName) instead of COUNT(*)?
      COUNT(OwnerIdName) AS [Activity Related]
   FROM dbo.ActivityPointer 
   GROUP BY OwnerId
 ) AS ar
ON su.SystemUser.SystemUserId = ar.OwnerId 
INNER JOIN
 (
   SELECT OwnerId,
      COUNT(DISTINCT dbo.new_troubletickettracking.OwnerIdName) AS [Test]
   FROM dbo.new_troubletickettracking 
   GROUP BY OwnerId 
 ) AS tt
ON su.SystemUser.SystemUserId = tt.OwnerId 

答案 1 :(得分:0)

您可以使用子查询,如下面的查询:

SELECT
    [Helpdesk] = su.FullName,
    [Activity Related] = (SELECT COUNT(ap.OwnerIdName) FROM dbo.ActivityPointer ap WHERE ap.OwnerId = su.SystemUserId ),
    [Test] = (SELECT COUNT(tt.OwnerIdName) FROM dbo.new_troubletickettracking tt WHERE tt.OwnerId = su.SystemUserId )
FROM
    dbo.SystemUser su

答案 2 :(得分:0)

以下是样本数据

Table1(Name: Tab1)
======
ID            Name
1           srinath
2           srinath1
3           srinath2

Table2(Name: Tab2)
======
userid      name
1           srinath
1           srinath
2           srinath1
2           srinath1
2           srinath1
2           srinath1

Table3(Name: Tab3)
======
userid      name    
1           srinath
2           srinath1
3           srinath2
3           srinath2
3           srinath2
3           srinath2
3           srinath2
3           srinath2

以下是查询

;with cte as
(select * from Tab1),
cte1 as (select userid,count(userid) as cnt from Tab2 group by userid),
cte2 as (select userid,count(userid) as cnt from Tab3 group by userid)
select cte.id,cte.name,cte1.cnt,cte2.cnt from cte left join cte1 on cte.id = cte1.userid left join cte2 on cte.id = cte2.userid

Out Put

 UserID    Name        Cnt1   Cnt2
    1     srinath       2       1
    2     srinath1      4       1
    3      srinath2     NULL    6

以下是具有您的实际表名称的查询

;with cte as
(select * from SystemUser),
cte1 as (select OwnerId,count(OwnerId) as cnt from ActivityPointer group by OwnerId),
cte2 as (select OwnerId,count(OwnerId) as cnt from new_troubletickettracking group by OwnerId)
select cte.FullName as [Helpdesk],cte1.cnt,cte2.cnt from cte left join cte1 on cte.SystemUserId = cte1.OwnerId left join cte2 on cte.SystemUserId = cte2.OwnerId

只需复制上面的查询并执行即可获得所需的输出。

答案 3 :(得分:0)

;with cte as
(select * from SystemUser),
cte1 as (select OwnerId,count(OwnerId) as cnt from ActivityPointer group by OwnerId),
cte2 as (select OwnerId,count(OwnerId) as cnt from new_troubletickettracking group by OwnerId)
select cte.FullName as [Helpdesk],cte1.cnt [Activity Related],cte2.cnt [Test] from cte left join cte1 on cte.SystemUserId = cte1.OwnerId left join cte2 on cte.SystemUserId = cte2.OwnerId