初学者SQL问题:具有多个COUNT(*)结果的算术

时间:2010-05-29 07:24:51

标签: sql statistics count

继续使用Stack Exchange Data Explorer学习SQL的精神(参见:Can we become our own “Northwind” for teaching SQL / databases?),我决定尝试编写一个查询来回答一个简单的问题(在meta上):{{ 3}}

这就是我所做的:

What % of stackoverflow users have over 10,000 rep?

SELECT COUNT(*)
FROM Users
WHERE
  Users.Reputation >= 10000

结果:

556

Query#1

SELECT COUNT(*)
FROM
  USERS

结果:

227691

现在,我如何将它们组合成一个查询?这个查询成语叫什么?我需要写什么,所以我可以得到一行三列结果:

556     227691      0,00244190592

6 个答案:

答案 0 :(得分:11)

您可以使用Common Table Expression (CTE)

WITH c1 AS (
    SELECT COUNT(*) AS cnt
    FROM Users
    WHERE Users.Reputation >= 10000
), c2 AS (
    SELECT COUNT(*) AS cnt
    FROM Users
)
SELECT c1.cnt, c2.cnt, CAST(c1.cnt AS FLOAT) / c2.cnt
FROM c1, c2

答案 1 :(得分:3)

除了using CTEs之外,在这种情况下你也可以做到:

SELECT CAST((SELECT COUNT(*) FROM Users WHERE Users.Reputation >= 10000) AS float)  /
       (SELECT COUNT(*) FROM USERS) * 100  AS Percentage​

作为float的强制转换是强制浮点除法,因为整数除法556/227691会给出0。

答案 2 :(得分:3)

感谢此处的其他答案,我写了以下查询,所有这些查询都适用于SEDE:

“内联视图”

SELECT *, CAST([10K] AS FLOAT)/[All] AS [Ratio]
FROM (
   SELECT
    (SELECT COUNT(*) FROM Users) AS [All],
    (SELECT COUNT(*) FROM Users Where Reputation >= 10000) AS [10K]
) AS UsersCount

See query result


变量

DECLARE @numAll FLOAT
DECLARE @num10kers FLOAT

SET @numAll = (SELECT COUNT(*) FROM Users)
SET @num10kers = (SELECT COUNT(*) FROM Users WHERE Users.Reputation >= 10000);

SELECT  @num10kers AS [10K], @numAll AS [All], @num10Kers/@numAll AS [Ratio]

See query result

参考


公用表表达式

WITH Users10K AS ( 
    SELECT COUNT(*) AS Count
    FROM Users
    WHERE Users.Reputation >= 10000
), UsersAll AS (
    SELECT COUNT(*) As Count
    FROM Users
)
SELECT
    Users10K.Count AS [10K],
    UsersAll.Count AS [All],
    CAST(Users10K.Count AS FLOAT) / UsersAll.Count AS [Ratio]
FROM Users10K, UsersAll

See query result

参考

答案 3 :(得分:3)

对于这样的查询,我根据不同的标准对单个表进行多次计数,我喜欢使用SUMCASE

SELECT
    UsersCount.[10K],
    UsersCount.[All],
    (CAST(UsersCount.[10K] AS FLOAT) / UsersCount.[All]) AS [Ratio]
FROM
    (SELECT
         SUM(CASE
               WHEN Users.Reputation >= 10000 THEN 1
               ELSE 0
             END) AS [10K],
         COUNT(*) AS [All]
     FROM Users) AS UsersCount

query results

优点是您只扫描一次Users表,这可能会快得多。

答案 4 :(得分:2)

WITH tmp as (
SELECT COUNT(ID) AS repCount, (SELECT COUNT(ID) FROM Users ) AS totalCount
FROM Users
WHERE Users.Reputation > 10000
)
SELECT tmp.repCount, tmp.totalCount, (cast(tmp.repCount as decimal(10,2))/tmp.TotalCount) * 100 AS Percentage
FROM tmp

更新:没有带

SELECT COUNT(ID) AS repCount, (SELECT COUNT(ID) FROM Users ) AS totalCount, 
    (CAST((SELECT COUNT(ID) FROM Users WHERE Users.Reputation > 10000) AS DECIMAL(10,2)) /
        (SELECT COUNT(ID) FROM Users )) * 100 AS Persantage
FROM Users

答案 5 :(得分:2)

在MySQL中使用variables

SELECT @a:=(SELECT COUNT(*) FROM Users WHERE Users.Reputation >= 10000),
       @b:=(SELECT COUNT(*) FROM Users),
       IF(@b > 0, @a/@b, "--invalid--")
FROM Users
LIMIT 0,1