将三个计数查询合并为一个结果

时间:2015-02-28 23:41:38

标签: sql count

我正在学习,但我已经达到了自我成长的极限。我希望下面有三个查询,显示在一个查询中。但我迷失在联合和工会的术语中。

SELECT    DID.display as "DisplayID",
          COUNT(AltID.REFID) as "C_AltIDs"
FROM      DID,
          AltID
WHERE     DID.REFID = AltID.REFID
GROUP BY  AltID.REFID, DID.display 
ORDER BY  COUNT(AltID.REFID) desc;


SELECT    DID.display as "DisplayID",
          COUNT(address.REFID) as "C_AltAddresses"
FROM      DID,
          address
WHERE     DID.REFID = address.REFID
GROUP BY  address.REFID, DID.display 
ORDER BY  COUNT(address.REFID) desc;


SELECT    DID.display as "DisplayID",
          COUNT(name.REFID) as "C_AltNames"
FROM      DID,
          name
WHERE     DID.REFID = name.REFID
GROUP BY  name.REFID, DID.display 
ORDER BY  COUNT(name.REFID) desc;



**Desired end result**
|DisplayID|AltIDs|AltAddresses|AltNames|
    01       32        32         2
    02        2        12         4

2 个答案:

答案 0 :(得分:0)

肯定会让人感到困惑。如果要将结果(或表)连接在一起,请以这种方式考虑:

  1. 如果您希望结果位于同一行,那么您需要加入
  2. 如果您希望将结果叠加在一起,那么您需要一个UNION
  3. 在你的情况下,你想要并排,所以你必须将所有这些加在一起。

    SELECT
        t1.display,
        t1.C_AltIDs,
        t2.C_AltAddresses,
        t3.C_AltNames
    FROM
        (
            SELECT    DID.display as "DisplayID",
                  COUNT(AltID.REFID) as "C_AltIDs"
            FROM      DID,
                  AltID
            WHERE     DID.REFID = AltID.REFID
            GROUP BY  DID.display 
        ) t1
    
        LEFT OUTER JOIN
        (
    
            SELECT    DID.display as "DisplayID",
                  COUNT(address.REFID) as "C_AltAddresses"
            FROM      DID,
                  address
            WHERE     DID.REFID = address.REFID
            GROUP BY  DID.display 
        ) t2 ON
            t1.Display = t2.Display
    
        LEFT OUTER JOIN
        (
            SELECT    DID.display as "DisplayID",
                  COUNT(name.REFID) as "C_AltNames"
            FROM      DID,
                  name
            WHERE     DID.REFID = name.REFID
            GROUP BY  DID.display 
        ) t3 ON
            t1.Display = t3.Display
    

    我已从每个子查询中删除了ORDER BY子句,因为您无法在子查询中执行ORDER BY。我还从COUNT()条款中删除了与GROUP BY汇总的字段,因为如果您想要计算它们,那么将它们放在那里是没有意义的。出于某种原因,MySQL允许您使用类似时髦的东西,即使它是荒谬的,如果您在GROUP BY已经聚合的字段上尝试SELECT,大多数RDBMS都会引发错误子句。

答案 1 :(得分:0)

使用相关子查询可能对你有用吗?

SELECT    
  DID.display as "DisplayID",
  (SELECT COUNT(AltID.REFID) FROM AltID WHERE REFID = DID.REFID) as "C_AltIDs",
  (SELECT COUNT(address.REFID) FROM address WHERE REFID = DID.REFID) as "C_AltAddresses",
  (SELECT COUNT(name.REFID) FROM name WHERE REFID = DID.REFID) as "C_AltNames" 
FROM DID
ORDER BY display 

编辑:评论中要求提供的其他解决方案:

;WITH x AS (
  SELECT    
    DID.display as "DisplayID",
    (SELECT COUNT(AltID.REFID) FROM AltID WHERE REFID = DID.REFID) as "C_AltIDs",
    (SELECT COUNT(address.REFID) FROM address WHERE REFID = DID.REFID) as "C_AltAddresses",
    (SELECT COUNT(name.REFID) FROM name WHERE REFID = DID.REFID) as "C_AltNames" 
  FROM DID
) 

SELECT *, C_AltIDs+C_AltAddresses+C_AltNames AS SumOfCounts 
FROM x