SQL:来自同一个表的多个计数

时间:2015-02-05 16:21:36

标签: sql sql-server sql-server-2008

我在尝试使用我需要的数据进行查询时遇到了实际问题。我尝试了一些没有成功的方法。我可以通过4个单独的查询获取数据,只是无法获得1个查询。所有数据均来自1个表格。我将列出尽可能多的信息。

我的数据看起来像这样。我有一个customerID和3列记录了该客户的记录以及指定的帐户管理员

 RecID_Customer___CreatedBy____LastUser____AcctMan

 1-------1374----------Bob Jones--------Mary Willis------Bob Jones

 2-------1375----------Mary Willis------Bob Jones--------Bob Jones

 3-------1376----------Jay Scott--------Mary Willis-------Mary Willis

 4-------1377----------Jay Scott--------Mary Willis------Jay Scott

 5-------1378----------Bob Jones--------Jay Scott--------Jay Scott

我希望查询返回以下数据。请参阅下文,了解如何获得每种方法。

Employee___Created__Modified__Mod Own__Created Own 

 Bob Jones--------2-----------1---------------1----------------1

 Mary Willis------1-----------2---------------1----------------0

 Jay Scott--------2-----------1---------------1----------------1
  

Created =计算每个员工创建的记录数

     

已修改=员工列为最后用户的记录数   (除非他们创建了记录)

     

Mod Own = LastUser = Acctman的每个记录数   (客户经理)

     

Created Own =员工创建的记录数量   该客户的客户经理

我可以从查询中获取每一个,只需要以某种方式组合它们:

Select CreatedBy, COUNT(CreatedBy) as Created
FROM [dbo].[Cust_REc] GROUP By CreatedBy

Select LastUser, COUNT(LastUser) as Modified
FROM [dbo].[Cust_REc] Where LastUser != CreatedBy GROUP By LastUser

Select AcctMan, COUNT(AcctMan) as CreatePort
FROM [dbo].[Cust_REc] Where AcctMan = CreatedBy GROUP By AcctMan

Select AcctMan, COUNT(AcctMan) as ModPort
FROM [dbo].[Cust_REc] Where AcctMan = LastUser AND NOT AcctMan = CreatedBy GROUP By AcctMan

有人能看到这样做的方法吗?我可能不得不加入桌子,但我的尝试没有给我正确的数据。

4 个答案:

答案 0 :(得分:1)

以下内容将为您提供您正在寻找的结果。

select 
    e.employee,
    create_count=(select count(*) from customers c where c.createdby=e.employee),
    mod_count=(select count(*) from customers c where c.lastmodifiedby=e.employee),
    create_own_count=(select count(*) from customers c where c.createdby=e.employee and c.acctman=e.employee),
    mod_own_count=(select count(*) from customers c where c.lastmodifiedby=e.employee and c.acctman=e.employee)
from (
    select employee=createdby from customers
    union
    select employee=lastmodifiedby from customers
    union
    select employee=acctman from customers
) e

注意:还有其他方法比这更有效,但也可能更复杂。具体来说,我敢打赌,在某个地方有一个主Employee表会阻止你只是为了获取名字列表而进行内联视图。

答案 1 :(得分:0)

这看起来非常简单。试试这个:

select a.employee,b.created,c.modified ....
from (select distinct created_by from data) as a
inner join
     (select created_by,count(*) as created from data group by created_by) as b
on a.employee = b.created_by)

inner join ....

答案 2 :(得分:0)

这种非常低效的查询可能是您正在寻找的一个粗略的开始。一旦验证了数据,就可以做一些事情来整理它并提高效率。

此外,我认为您不需要DISTINCT部分UNION,因为UNION将返回DISTINCT值,除非指定UNION ALL

SELECT  
    Employees.EmployeeID,
    Created   =(SELECT COUNT(*) FROM Cust_REc WHERE Cust_REc.CreatedBy=Employees.EmployeeID),
    Mopdified =(SELECT COUNT(*) FROM Cust_REc WHERE Cust_REc.LastUser=Employees.EmployeeID AND Cust_REc.CreateBy<>Employees.EmployeeID),
    ModOwn    =
        CASE WHEN NOT Empoyees.IsManager THEN NULL ELSE
            (SELECT COUNT(*) FROM Cust_REc WHERE AcctMan=Employees.EmployeeID)
        END,
    CreatedOwn=(SELECT COUNT(*) FROM Cust_REc WHERE AcctMan=Employees.EmployeeID AND CReatedBy=Employees.EMployeeID)
FROM
    (       
        SELECT
            EmployeeID,
            IsManager=CASE WHEN EXISTS(SELECT AcctMan FROM CustRec WHERE AcctMan=EmployeeID)
        FROM
        (
            SELECT DISTINCT 
                EmployeeID
            FROM
            (
                SELECT EmployeeID=CreatedBy FROM Cust_Rec
                UNION
                SELECT EmployeeID=LastUser FROM Cust_Rec
                UNION
                SELECT EmployeeID=AcctMan FROM Cust_Rec
            )AS Z
        )AS Y
    )
    AS Employees

答案 3 :(得分:0)

我在修改列中遇到了同样的问题。所有其他列都运行正常。如果你有的话,DCR示例可以很好地与员工表上的联接一起使用。

SELECT  CreatedBy AS [Employee],
        COUNT(CreatedBy) AS [Created],
        --Couldn't get modified to pull the right results
        SUM(CASE WHEN LastUser = AcctMan THEN 1 ELSE 0 END) [Mod Own],
        SUM(CASE WHEN CreatedBy = AcctMan THEN 1 ELSE 0 END) [Created Own]
FROM Cust_Rec
GROUP BY CreatedBy