sql - SQL 显示每行重复记录的总数
问题描述
我想添加一列,显示“名称”在其他行中出现的次数。基本上是该名称出现的总次数,但列在每一行。
select number, name from table;
添加前的样本数据:
number name
1234 storeA
1235 storeA
1236 storeA
1237 storeB
1238 storeC
1239 storeC
添加后所需的输出
number name Total
1234 storeA 3
1235 storeA 3
1236 storeA 3
1237 storeB 1
1238 storeC 2
1239 storeC 2
解决方案
您可以使用窗口功能:
select t.*,
count(*) over (partition by name) as total
from table t;
但是,ANSI
如果窗口函数不起作用,SQL 标准会提供关联子查询方法:
select t.*,
(select count(*)
from table t1
where t1.name = t.name
) as total
from table t;
推荐阅读
- javascript - 如何获取 LG-WebOS 智能电视的 MAC 地址?
- python - 使用 beautifulsoup 抓取元素列表
- java - 如果在 if 中调用方法,则方法不执行任何操作,如果直接从 on 事件调用,则它可以工作
- html - Html5 画布元素像素网格(pageX 和 pageY)
- jquery - 前端:给选项,但点击后空白默认有选项或空白输入,但如果我们点击有列表
- function - Yii2,重定向在静态函数中不起作用
- ruby-on-rails - 我们如何循环用户注册表单 url 并通过编码在表单中填写数据并在 rails 中提交表单?
- excel - 为 AGGREGATE 公式查找关联单元格以确定列的顶部差异
- umbraco - 无法登录管理页面 umbraco 后台
- python - 用于可视化循环 ETA 的进度条