sql - SQL Server 中的计数总和
问题描述
我有这样的数据,
id state country name years
1 ohio US steve 2020-08-11
2 delhi india priya 2019-09-01
3 ohio US alex 2020-07-11
4 NY US kristen 2018-06-11
5 hyderabad india riya 2019-08-16
6 hyderabad india isha 2019-05-04
select (count(*)) as 'Counts',country, state
from dbo.people
group by country, state
having count(*)>1
我写了上面的查询来计算住在同一州的人
counts country state
2 india hyderabad
2 US ohio
你能帮我写一个查询,它会返回计数的总和,然后在总和中加 1,在此先感谢
解决方案
您可以将初始查询设置为CTE(公用表表达式)以求和它们的计数:
with initial_query as (
select (count(*)) as 'Counts',country, state
from dbo.people
group by country, state
having count(*)>1
)
select sum(Counts) + 1 as Counts
from initial_query
推荐阅读
- vb.net - 如何使用 vb.net 从字符串中消除多余元素
- python - 绘制 PyqtGraph 字符串和浮点数
- java - 如何在构建后脚本中访问调用程序集成测试的控制台输出?
- git - 更改推送建议的默认遥控器
- unit-testing - vstest.console 进程在 90 秒后无法连接到 testhost 进程
- r - 数据框中从长格式到宽格式
- python - Python:将列表添加到数组
- annotations - 将 Yolov5 bbox 导出为 Pascal 格式
- excel - 在 Excel VBA 宏中组合两种类型的粘贴
- reactjs - 材质 UI 步进样式不变