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

时间:2010-04-27 15:15:18

标签: sql count group-by

我有一个具有group by的sql select查询。 我想在group by语句后计算所有记录。 有没有办法直接从sql? 例如,拥有一个包含用户的表我想选择不同的城镇和用户数

select town, count(*) from user
group by town

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

总共3个城镇和58个用户的结果示例是:

Town         Count
Copenhagen   58
NewYork      58
Athens       58

11 个答案:

答案 0 :(得分:260)

这将做你想要的(城镇列表,每个城镇的用户数量):

select town, count(town) 
from user
group by town

使用GROUP BY时,您可以使用大多数aggregate functions

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

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

DECLARE @numOfUsers INT
SET @numOfUsers = SELECT COUNT(*) FROM user

SELECT DISTINCT town, @numOfUsers
FROM user

答案 1 :(得分:136)

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

SELECT COUNT(DISTINCT town) 
FROM user

答案 2 :(得分:31)

另一种方式是:

/* 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

答案 3 :(得分:4)

如果你想按计数订购(声音很简单,但我找不到如何做到这一点的答案),你可以这样做:

        SELECT town, count(town) as total FROM user
        GROUP BY town ORDER BY total DESC

答案 4 :(得分:3)

使用Oracle,您可以使用分析函数:

select town, count(town), sum(count(town)) over () total_count from user
group by town

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

select town, count(town), (select count(town) from user) as total_count from user
group by town

答案 5 :(得分:2)

您可以在COUNT中使用DISTINCT,就像Milkovsky所说的那样

就我而言:

select COUNT(distinct user_id) from answers_votes where answer_id in (694,695);

这将把被认为与一个计数相同的user_id的答案投票数量

答案 6 :(得分:2)

我知道这是一篇旧帖子,在SQL Server中:

select  isnull(town,'TOTAL') Town, count(*) cnt
from    user
group by town WITH ROLLUP

Town         cnt
Copenhagen   58
NewYork      58
Athens       58
TOTAL        174

答案 7 :(得分:1)

如果您想选择城镇和总用户数,可以使用以下查询:

SELECT Town, (SELECT Count(*) FROM User) `Count` FROM user GROUP BY Town;

答案 8 :(得分:1)

十个未删除的答案;大多数执行用户要求的操作。大多数答案误认为该问题是因为每个镇上有58位用户,而不是58位用户。即使是少数正确的方法也不是最佳选择。

Select tag_id, 
group_concat(distinct pvid_id) 
from your_table where tag_id 
in('1','2')
group by tag_id

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)

SHOW session status LIKE 'Handler%';

在OP的上下文中:

+----------------------------+-------+
| 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  
+----------------------------+-------+

由于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)是否不为null,在这种情况下是不必要的。

答案 9 :(得分:0)

请尝试以下代码:

select ccode, count(empno) 
from company_details 
group by ccode;

答案 10 :(得分:0)

如果您想使用Select Count Query With Count Option,请尝试此...

 select a.*, (Select count(b.name) from table_name as b where Condition) as totCount from table_name  as a where where Condition