MySQL中所有行的count(*)之和

时间:2019-06-12 08:17:21

标签: mysql sql database rdbms

我陷入了.as-console-wrapper { max-height: 100% !important; }查询的问题,我想在所有sum()行中添加count(*)个值的总和。

以下是查询:

group by

当前输出:

select
    u.user_type as user,
    u.count,
    sum(u.count)
FROM
(
select 
    DISTINCT
    user_type,
    count(*) as count
FROM
    users
where
    (user_type = "driver" OR user_type = "passenger")
GROUP BY
    user_type
) u;

预期输出:

----------------------------------
|  user     |  count     |  sum  |
----------------------------------
| driver    | 58         |  90   |
----------------------------------

如果我从查询中删除---------------------------------- | user | count | sum | ---------------------------------- | driver | 58 | 90 | | passenger | 32 | 90 | ---------------------------------- ,则输出如下:

sum(u.count)

9 个答案:

答案 0 :(得分:8)

您需要一个子查询:

   SELECT user_type, 
          Count(*) AS count,
          (SELECT COUNT(*) 
           FROM users 
           WHERE user_type IN ("driver","passenger" )) as sum  
   FROM   users 
   WHERE  user_type IN ("driver","passenger" ) 
   GROUP  BY user_type ; 

请注意,您在这里不需要distinct

OR

   SELECT user_type, 
          Count(*) AS count,
          c.sum
   FROM   users 
   CROSS JOIN ( 
           SELECT COUNT(*) as sum
           FROM users 
           WHERE user_type IN ("driver","passenger" )
         ) as c

   WHERE  user_type IN ("driver","passenger" ) 
   GROUP  BY user_type ; 

答案 1 :(得分:7)

您可以使用WITH ROLLUP modifier

select coalesce(user_type, 'total') as user, count(*) as count
from users
where user_type in ('driver', 'passenger')
group by user_type with rollup

这将返回相同的信息,但格式不同:

user      | count
----------|------
driver    |    32
passenger |    58
total     |    90

db-fiddle

在MySQL 8中,您可以将COUNT()用作window function

select distinct
  user_type,
  count(*) over (partition by user_type) as count,
  count(*) over () as sum
from users
where user_type in ('driver', 'passenger');

结果:

user_type | count | sum
----------|-------|----
driver    |    32 |  90
passenger |    58 |  90

db-fiddle

或使用CTE (Common Table Expressions)

with cte as (
  select user_type, count(*) as count
  from users
  where user_type in ('driver', 'passenger')
  group by user_type
)
select user_type, count, (select sum(count) from cte) as sum
from cte

db-fiddle

答案 2 :(得分:6)

尝试一下。内联视图获取整体总数:

SELECT a.user_type, 
       count(*) AS count,
       b.sum  
FROM   users a
JOIN (SELECT COUNT(*) as sum 
      FROM users 
      WHERE user_type IN ("driver","passenger" )
     ) b ON TRUE
WHERE  a.user_type IN ("driver","passenger" ) 
GROUP  BY a.user_type;

答案 3 :(得分:6)

您可以将SUM() OVER()COUNT(*)组合在一起:

SELECT user_type, COUNT(*) AS cnt, SUM(COUNT(*)) OVER() AS total
FROM  users WHERE user_type IN ('driver', 'passenger') GROUP BY user_type;

db<>fiddle demo


输出:

+------------+------+-------+
| user_type  | cnt  | total |
+------------+------+-------+
| passenger  |  58  |    90 |
| driver     |  32  |    90 |
+------------+------+-------+

答案 4 :(得分:5)

我很想问; 您确定需要在数据库级别吗?

除非您仅在数据库层中工作,否则对这些结果的任何处理都将内置到应用程序层中,并且可能需要对结果进行某种形式的循环

运行起来可能更容易,更简单且更具可读性

  SELECT user_type,
         COUNT(*) AS count
    FROM users
   WHERE user_type IN ("driver", "passenger")
GROUP BY user_type

..并简单地将应用层中的总数相加

Juan在另一个答案中指出,DISTINCT是多余的,因为GROUP BY确保每个结果行都是不同的

像Juan一样,我也更喜欢在这里使用IN而不是OR条件,因为user_type更具可读性。如果将来再结合AND条件,还可以减少混淆的可能性

顺便说一句,我考虑将用户类型的名称“驱动程序”和“乘客”移到单独的user_types表中,并通过用户表中的ID列对其进行引用

NB 如果您确实需要数据库级别的支持,我建议您使用Paul的出色选择之一,或者使用Tom Mac和Juan推荐的CROSS JOIN方法作为第二个建议的解决方案

答案 5 :(得分:2)

group by的末尾添加user-type子句,例如:

select
    u.user_type as user,
    u.count,
    sum(u.count)
FROM
(
select 
    DISTINCT
    user_type,
    count(*) as count
FROM
    users
where
    (user_type = "driver" OR user_type = "passenger")
GROUP BY
    user_type
) u GROUP BY u.user_type;

答案 6 :(得分:1)

Tom Mac正确解释您的答案。这是您可以执行此操作的另一种方法。 我检查了查询性能,但在1000条记录中没有发现任何差异

   select user_type,Countuser,(SELECT COUNT(*) 
   FROM users 
   WHERE user_type IN ('driver','passenger ') )as sum from (
   select user_type,count(*) as Countuser from users a
   where a.user_type='driver'
   group by a.user_type
   union
   select user_type,count(*) as Countuser from users b
   where b.user_type='passenger'
   group by b.user_type
   )c
   group by user_type,Countuser

答案 7 :(得分:0)

select
    u.user_type as user,
    u.count,
    sum(u.count)
FROM users group by user

答案 8 :(得分:0)

尝试一下:

WITH SUB_Q AS (
  SELECT USER_TYPE, COUNT (*) AS CNT
  FROM USERS
  WHERE USER_TYPE = "passenger" OR USER_TYPE = "driver"
  GROUP BY USER_TYPE
), 
SUB_Q2 AS (
  SELECT SUM(CNT) AS SUM_OF_COUNT
  FROM SUB_Q
)
SELECT A.USER_TYPE, A.CNT AS COUNT, SUB_Q2 AS SUM
FROM SUB_Q JOIN SUB_Q2 ON (TRUE);

我使用了postgresql方言,但是您可以轻松地更改为子查询。