0

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

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

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 
4

3 回答 3

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 
于 2013-06-27T15:50:19.917 回答
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
于 2013-06-27T15:56:53.267 回答
0

尝试通过 CompanyId 添加一个组,让我们知道它是如何工作的。

于 2013-06-27T15:57:06.003 回答