关于sql:如何在同一个select语句中使用count和group by

How to use count and group by at the same select statement

我有一个SQL选择查询,它有一个分组依据。我想计算group by语句之后的所有记录。有没有直接从SQL实现的方法?例如,如果有一个包含用户的表,我想选择不同的城镇和用户总数

1
2
SELECT town, COUNT(*) FROM USER
GROUP BY town

我想要一个包含所有城镇的列,另一个包含所有行中的用户数。

有3个镇和58个用户的结果示例如下:

1
2
3
4
Town         COUNT
Copenhagen   58
NewYork      58
Athens       58


这将满足您的需求(城镇列表,以及每个城镇的用户数):

1
2
3
SELECT town, COUNT(town)
FROM USER
GROUP BY town

使用GROUP BY时,可以使用大多数聚合函数。

更新(更改问题和评论后)

您可以为用户数声明一个变量,并将其设置为用户数,然后使用该变量进行选择。

1
2
3
4
5
DECLARE @numOfUsers INT
SET @numOfUsers = SELECT COUNT(*) FROM USER

SELECT DISTINCT town, @numOfUsers
FROM USER


您可以使用COUNT(DISTINCT ...)

1
2
SELECT COUNT(DISTINCT town)
FROM USER


另一种方法是:

1
2
3
4
5
6
7
8
/* Number of rows in a derived table called d1. */
SELECT COUNT(*) FROM
(
  /* Number of times each town appears in user. */
  SELECT town, COUNT(*)
  FROM USER
  GROUP BY town
) d1


如果您想按计数排序(听起来很简单,但我在如何排序的堆栈上找不到答案),可以执行以下操作:

1
2
        SELECT town, COUNT(town) AS total FROM USER
        GROUP BY town ORDER BY total DESC


使用Oracle,您可以使用分析功能:

1
2
SELECT town, COUNT(town), SUM(COUNT(town)) OVER () total_count FROM USER
GROUP BY town

您的其他选项是使用子查询:

1
2
SELECT town, COUNT(town), (SELECT COUNT(town) FROM USER) AS total_count FROM USER
GROUP BY town


我知道这是一篇老文章,在SQL Server中:

1
2
3
4
5
6
7
8
9
SELECT  isnull(town,'TOTAL') Town, COUNT(*) cnt
FROM    USER
GROUP BY town WITH ROLLUP

Town         cnt
Copenhagen   58
NewYork      58
Athens       58
TOTAL        174


你可以像米尔科夫斯基所说的那样在伯爵内部使用独特的

就我而言:

1
SELECT COUNT(DISTINCT user_id) FROM answers_votes WHERE answer_id IN (694,695);

这将拉取与一个计数相同的用户ID的应答投票计数。


十个未删除的答案;大多数不做用户要求的。大多数回答错误地理解了这个问题,认为每个城镇有58个用户,而不是总共58个。即使是少数正确的也不是最佳的。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
mysql> FLUSH STATUS;
Query OK, 0 ROWS affected (0.00 sec)

SELECT  province, total_cities
    FROM       ( SELECT  DISTINCT province  FROM  canada ) AS provinces
    CROSS JOIN ( SELECT  COUNT(*) total_cities  FROM  canada ) AS tot;
+---------------------------+--------------+
| province                  | total_cities |
+---------------------------+--------------+
| Alberta                   |         5484 |
| British Columbia          |         5484 |
| Manitoba                  |         5484 |
| NEW Brunswick             |         5484 |
| Newfoundland AND Labrador |         5484 |
| Northwest Territories     |         5484 |
| Nova Scotia               |         5484 |
| Nunavut                   |         5484 |
| Ontario                   |         5484 |
| Prince Edward Island      |         5484 |
| Quebec                    |         5484 |
| Saskatchewan              |         5484 |
| Yukon                     |         5484 |
+---------------------------+--------------+
13 ROWS IN SET (0.01 sec)

埃多克斯1〔2〕

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
+----------------------------+-------+
| Variable_name              | VALUE |
+----------------------------+-------+
| Handler_commit             | 1     |
| Handler_delete             | 0     |
| Handler_discover           | 0     |
| Handler_external_lock      | 4     |
| Handler_mrr_init           | 0     |
| Handler_prepare            | 0     |
| Handler_read_first         | 3     |
| Handler_read_key           | 16    |
| Handler_read_last          | 1     |
| Handler_read_next          | 5484  |  -- One table scan to get COUNT(*)
| Handler_read_prev          | 0     |
| Handler_read_rnd           | 0     |
| Handler_read_rnd_next      | 15    |
| Handler_rollback           | 0     |
| Handler_savepoint          | 0     |
| Handler_savepoint_rollback | 0     |
| Handler_update             | 0     |
| Handler_write              | 14    |  -- leapfrog through index to find provinces  
+----------------------------+-------+

在操作环境中:

1
2
3
SELECT  town, total_users
    FROM       ( SELECT  DISTINCT town  FROM  canada ) AS towns
    CROSS JOIN ( SELECT  COUNT(*) total_users  FROM  canada ) AS tot;

由于tot只有一行,因此CROSS JOIN的体积并不像其他的那样大。

通常的模式是COUNT(*),而不是COUNT(town)。后者意味着检查town是否不为空,这在本文中是不必要的。


如果要选择"城镇"和"用户总数",可以使用下面的查询:

1
SELECT Town, (SELECT COUNT(*) FROM USER) `Count` FROM USER GROUP BY Town;


如果要使用Select all query with count选项,请尝试此操作…

1
 SELECT a.*, (SELECT COUNT(b.name) FROM TABLE_NAME AS b WHERE Condition) AS totCount FROM TABLE_NAME  AS a WHERE WHERE Condition


尝试以下代码:

1
2
3
SELECT ccode, COUNT(empno)
FROM company_details
GROUP BY ccode;