加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

TSQL模式(平均值,中位数,模式)

发布时间:2020-12-12 06:31:41 所属栏目:MsSql教程 来源:网络整理
导读:我正在尝试计算一个表中的一系列idsofInterest的模式,每个都带有一个伴随的valueOfInterest: idsOfInterest | valueOfInterest 2 | 1A 2 | 1A 2 | 3B 1 | 2A 1 | 2C 1 | 2A 4 | 3B 4 | 3B 4 | 4C 但有数百万行. 每个idOfInterest列表都足够长,多模式不是问题
我正在尝试计算一个表中的一系列idsofInterest的模式,每个都带有一个伴随的valueOfInterest:
idsOfInterest | valueOfInterest  
2             | 1A  
2             | 1A  
2             | 3B  
1             | 2A  
1             | 2C  
1             | 2A  
4             | 3B  
4             | 3B  
4             | 4C

但有数百万行.
每个idOfInterest列表都足够长,多模式不是问题.理想情况下,我想要像

idsOfInterest | modeValueOfInterest  
1             | 2A  
2             | 1A  
3             | 3C  
4             | 3B

任何帮助赞赏. (使用MS SQL Server 2008)

解决方法

该模式是最常见的值.你可以通过聚合和row_number()得到这个:
select idsOfInterest,valueOfInterest
from (select idsOfInterest,valueOfInterest,count(*) as cnt,row_number() over (partition by idsOfInterest order by count(*) desc) as seqnum
      from table t
      group by idsOfInterest,valueOfInterest
     ) t
where seqnum = 1;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读