子总和查询可以带来更多结果

时间:2013-06-27 15:42:40

标签: sql sql-server

我目前正在运行以下查询。(参见下文)但是,当我运行此查询时,活动用户和暂停用户会带回远远大于数据库中的结果。

我只是想知道你是否可以阐明原因并纠正我哪里出错?

SELECT c.[Status],
       c.CompanyId,
       c.Name,
       (SELECT count(DISTINCT usr.UserID)
        FROM   [ondemand.10cms.com].Security.[user] usr
               INNER JOIN [ondemand.10cms.com].Company.Company
                 ON usr.CompanyID = c.CompanyID) AS TotalUsers,
       (SELECT sum (CASE
                      WHEN usr.Status = 2 THEN 1
                      ELSE 0
                    END)
        FROM   [ondemand.10cms.com].Security.[user] usr
               INNER JOIN [ondemand.10cms.com].Company.Company
                 ON usr.CompanyID = c.CompanyID) AS ActiveUsers,
       (SELECT sum (CASE
                      WHEN usr.Status = 3 THEN 1
                      ELSE 0
                    END)
        FROM   [ondemand.10cms.com].Security.[User] usr
               INNER JOIN [ondemand.10cms.com].Company.Company
                 ON usr.CompanyID = c.CompanyID) AS SuspendedUsers
FROM   [ondemand.10cms.com].Company.Company c 

3 个答案:

答案 0 :(得分:3)

在每个子查询中,您有两个表,一个表连接到外部查询,但两个内部表之间没有连接。所有这些子查询都有点不必要,我会重写为更简单的查询,如下所示:

 SELECT 
  Company.[Status]
  ,Company.CompanyId
  ,Company.Name
  ,COUNT(DISTINCT usr.UserID) AS TotalUsers
  ,SUM(CASE WHEN usr.Status = 2 THEN 1
           ELSE 0
           END) AS ActiveUsers
  ,SUM(CASE WHEN usr.Status = 3 THEN 1
             ELSE 0
             END) AS SuspendedUsers
FROM [ondemand.10cms.com].Security.[user] usr
  INNER JOIN [ondemand.10cms.com].Company.Company
           ON usr.CompanyID = Company.CompanyID
GROUP BY
  Company.[Status]
  ,Company.CompanyId
  ,Company.Name

如果您只是按原样修复查询,请尝试以下操作:

SELECT c.[Status],
       c.CompanyId,
       c.Name,
       (SELECT count(DISTINCT usr.UserID)
        FROM   [ondemand.10cms.com].Security.[user] usr
               INNER JOIN [ondemand.10cms.com].Company.Company
                 ON usr.CompanyID = Company.CompanyID
        WHERE usr.CompanyID = c.CompanyID) AS TotalUsers,
       (SELECT sum (CASE
                      WHEN usr.Status = 2 THEN 1
                      ELSE 0
                    END)
        FROM   [ondemand.10cms.com].Security.[user] usr
               INNER JOIN [ondemand.10cms.com].Company.Company
                 ON usr.CompanyID = Company.CompanyID
        WHERE usr.CompanyID = c.CompanyID) AS ActiveUsers,
       (SELECT sum (CASE
                      WHEN usr.Status = 3 THEN 1
                      ELSE 0
                    END)
        FROM   [ondemand.10cms.com].Security.[User] usr
               INNER JOIN [ondemand.10cms.com].Company.Company
                 ON usr.CompanyID = Company.CompanyID
        WHERE usr.CompanyID = c.CompanyID) AS SuspendedUsers
FROM   [ondemand.10cms.com].Company.Company c 

答案 1 :(得分:0)

我不知道您的数据(可能会导致重复),但是您不能使用标准组而不使用子查询?

Select
    c.[Status],
    c.CompanyId,
    c.Name,
    count(distinct usr.UserID) as TotalUsers,
    sum(case when usr.Status = 2 then 1 else 0 end) as ActiveUsers,
    sum(case when usr.Status = 3 then 1 else 0 end) as SuspendedUsers
from [ondemand.10cms.com].Company.Company  c 
inner join [ondemand.10cms.com].Security.[user] usr   
    on  usr.CompanyID=c.CompanyID
GROUP BY
    c.[Status],
    c.CompanyId,
    c.Name

答案 2 :(得分:0)

尝试通过CompanyId添加一个组,并告诉我们它是如何工作的。