我该如何使用COUNT()?

时间:2013-12-04 20:27:31

标签: sql database

我正在尝试获取leaduredived表中的AgentID字段和leadtep字段。这些计数应按每个员工的日期计算。

我希望查询结果返回以下字段:

  • 日期
  • EmployeeLastName
  • EmployeeFirstName
  • AgentID计数
  • Leadstep伯爵

我已经单独运行查询并且可以获得正确的计数,但是当我尝试将计数组合到一个查询中时,计数不正确。

表#1:员工

AgentID              AgentFirstName              AgentLastName
12345                     John                       Smith
67891                     Jim                        Jones

表#2:收到的潜在客户

Acctdate                  AgentID
9/1/2013                   12345
9/1/2013                   12345
9/1/2013                   12345
9/1/2013                   67891  
9/1/2013                   67891  
9/1/2013                   67891  
9/1/2013                   67891  

表#3:提出的潜在客户

Acctdate                    AgentID                  Leadstep
9/1/2013                    12345                      ABC
9/1/2013                    12345                      ABC
9/1/2013                    12345                      DEF
9/1/2013                    12345                      DEF
9/1/2013                    12345                      DEF
9/1/2013                    67891                      DEF
9/1/2013                    67891                      DEF
9/1/2013                    67891                      DEF
9/1/2013                    67891                      ABC

以下是我正在使用的代码:

SELECT [Leads Received].ACCTGDATE, 
    Employees.AgentLastName, 
    Employees.AgentFirstName, 
    Count([Leads Proposed].LEADSTEP) AS CountOfLEADSTEP, 
    Count([Leads Received].AgentID) AS CountOfAgentID
FROM Employees 
INNER JOIN [Leads Proposed] 
    ON Employees.AgentID = [Leads Proposed].AgentID) 
INNER JOIN [Leads Received] ON 
    [Leads Proposed].ACCTGDATE = [Leads Received].ACCTGDATE 
    AND Employees.AgentID = [Leads Received].AgentID
GROUP BY [Leads Received].ACCTGDATE, 
    Employees.AgentLastName, 
    Employees.AgentFirstName;

对我做错了什么的想法?

3 个答案:

答案 0 :(得分:0)

我相信这样的事情应该有效:

SELECT  allComb.AcctDate
    , Employees.AgentLastName
    , Employees.AgentFirstName
    , lr.agentCount
    , lp.leadCount
FROM Employees
--get a list of all unique Agent and Date Combinations for both tables
LEFT JOIN (
    SELECT AcctDate, AgentId
    FROM LeadsReceived
    UNION
    SELECT AcctDate, AgentId
    FROM LeadsProposed
) AS allComb
ON employees.AgentId = allComb.AgentId
--Get the agent count from LeadsRecieved
LEFT JOIN (
    SELECT AgentId, AcctDate, COUNT(AgentId) AS agentCount
    FROM LeadsReceived
    GROUP BY AgentId, AcctDate
) AS lr
ON employees.AgentId = lr.AgentId
AND allComb.AcctDate = lr.AcctDate
--Get the LeadStep Count from LeadsProposed
LEFT JOIN (
    SELECT AgentId, AcctDate, COUNT(Leadstep) AS leadCount
    FROM LeadsProposed
    GROUP BY AgentId, AcctDate
) AS lp
ON employees.AgentId = lp.AgentId
AND allComb.AcctDate = lp.AcctDate

答案 1 :(得分:0)

SELECT ADATE AS ACCTGDATE,
    Employees.AgentLastName,
    Employees.AgentFirstName,
    (
       SELECT COUNT(DISTINCT LEADSTEP) FROM [Leads Proposed]
       -- SELECT COUNT(*) FROM [Leads Proposed]  -- or maybe this
        WHERE [Leads Proposed].AgentID = AID
          AND [Leads Proposed].Acctdate = ADATE
    ) AS CountOfLEADSTEP,
    (
       SELECT COUNT(*) FROM [Leads Received]
       WHERE [Leads Received].AgentID = AID
         AND [Leads Received].Acctdate = ADATE
    ) AS CountOfAgentID
FROM Employees
INNER JOIN (
  SELECT AgentID AS AID, Acctdate AS ADATE FROM [Leads Proposed]
  UNION
  SELECT AgentID AS AID, Acctdate AS ADATE FROM [Leads Received]
) AS a ON AgentID = AID
GROUP BY ADATE, AID

使用自动递增ID,查询变为:

SELECT
    [Leads Proposed].AcctDate AS ACCTGDATE,
    Employees.AgentLastName,
    Employees.AgentFirstName,
    COUNT(DISTINCT [Leads Proposed].ID) AS CountOfLEADSTEP,
    COUNT(DISTINCT [Leads Received].ID) AS CountOfAgent
FROM Employees
JOIN [Leads Proposed] ON [Leads Proposed].AgentID = Employees.AgentID
JOIN [Leads Received] ON [Leads Received].AgentID = Employees.AgentID
                     AND [Leads Proposed].AcctDate = [Leads Received].AcctDate
GROUP BY Employees.AgentID, [Leads Proposed].AcctDate

答案 2 :(得分:0)

这将有效

SELECT COALESCE(t3.AcctDate1, t3.AcctDate2), Employees.AgentLastName, Employees.AgentFirstName, 
        t3.CountOfLEADSTEP, t3.CountOfAgentID
FROM Employees
INNER JOIN 
(SELECT COALESCE(t1.AgentID, t2.AgentID) AS AgentID, t1.AcctDate AS AcctDate1, t1.CountOfAgentID, t2.AcctDate as AcctDate2, t2.CountOfLEADSTEP
    FROM
    (SELECT AcctDate, AgentID, Count([LeadsReceived].AgentID) AS CountOfAgentID FROM [LeadsReceived] GROUP BY AcctDate, AgentID) as t1
        FULL JOIN (SELECT AcctDate, AgentID, Count([LeadsProposed].LEADSTEP) AS CountOfLEADSTEP FROM [LeadsProposed] GROUP BY AcctDate, AgentID) as t2
        ON t1.AgentID = t2.AgentID AND t1.ACCTDATE = t2.ACCTDATE) AS t3
ON Employees.AgentID = t3.AgentID

从您提供的示例中,我在代理1234的LeadsReceived上为另一个日期添加了一行,在另一行上为另一个日期的同一代理提出了另一行,上述查询产生了以下结果,我认为是你在寻找什么。

2013-09-01 00:00:00.000 Smith   John    5   3
2013-09-01 00:00:00.000 Jones   Jim 4   4
2013-09-05 00:00:00.000 Smith   John    1   NULL
2013-09-20 00:00:00.000 Smith   John    NULL    1