我的任务是找出交易超过10笔的客户数量。我试过了,代码如下所示:
select
count(*) as Customer_More_10_Transaction
from dbo.Transactions
group by cust_id
having count (*) > 10输出只显示客户的交易,而不统计交易> 10的客户。我期望输出显示为36。
输出如下所示:
12
11
11
11
12
11
11
12
11
11
12
11
11
11
12
11
12
13
11
11
11
12
11
11
11
12
11
11
11
11
11
11
12
13
11
11实际需要的输出:
36发布于 2019-10-21 14:01:40
你还需要计算你的结果。
SELECT count(*) as Count_Of_Customer_More_10_Transaction FROM
(
select
count(*) as Customer_More_10_Transaction
from dbo.Transactions
group by cust_id
having count (*) > 10
) AS T发布于 2019-10-21 14:04:07
我们可以在没有子查询的情况下处理您的需求:
SELECT TOP 1 COUNT(*) OVER () AS total_cnt
FROM dbo.Transactions
GROUP BY cust_id
HAVING COUNT (*) > 10;发布于 2019-10-21 14:31:05
SELECT COUNT(*)
FROM
(
SELECT cust_id
FROM dbo.Transactions
group by cust_id
having count (*) > 10
) AS T获取事务数大于10的custid列表并统计。
https://stackoverflow.com/questions/58480516
复制相似问题