Group By 并获取最常出现的不同值

发布于 2024-12-20 02:04:17 字数 1409 浏览 1 评论 0原文

我想按 varchar 列进行分组并找到出现次数最多的外键值。问题是多个 fiModel 可以分配给同一个 TAC(称为 SSN_Number 的 15 位值的前 8 位)。

这是一个简化的模型和带有示例数据的查询:

create table #data(
    SSN_Number varchar(15),
    fiModel int
)
insert into #data
        SELECT '351806038155151',451 UNION ALL SELECT '353797028764243',232 UNION ALL SELECT '353797028764243',438 UNION ALL SELECT '353797028764243',438 UNION ALL SELECT '353797028764243',447 UNION ALL SELECT '358372015611578',318 UNION ALL SELECT '352045039834626',279 UNION ALL SELECT '352045031234567',279 UNION ALL SELECT '351806035647381',451 UNION ALL SELECT '352045037654321',207

--- following query returns all records(10)
select * from #data Order By SSN_Number

--- following query gives the distinct TAC's+fiModel, but TACs can repeat (9)
select substring(ssn_number,1,8)as TAC,fiModel,count(*) from #data
group by substring(ssn_number,1,8),fiModel
Order By substring(ssn_number,1,8),fiModel

--- following query gives the correct(distinct) TAC's (4), 
--- but i need the fiModel that occurs most often with the assigned TAC
--- if the number is the same, it doesn't matter what to take
select substring(ssn_number,1,8)as TAC,count(*) from #data
group by substring(ssn_number,1,8)
Order By substring(ssn_number,1,8)

drop table #data

所以这是期望的结果:

TAC         fiModel
35180603    451
35204503    279
35379702    438
35837201    318

I want to group by a varchar column and find the foreignkey value that occurs the most. The problem is that multiple fiModel can be assigned to the same TAC(first 8 digits of a 15-digit value called SSN_Number).

Here is a simplified model and query with sample-data:

create table #data(
    SSN_Number varchar(15),
    fiModel int
)
insert into #data
        SELECT '351806038155151',451 UNION ALL SELECT '353797028764243',232 UNION ALL SELECT '353797028764243',438 UNION ALL SELECT '353797028764243',438 UNION ALL SELECT '353797028764243',447 UNION ALL SELECT '358372015611578',318 UNION ALL SELECT '352045039834626',279 UNION ALL SELECT '352045031234567',279 UNION ALL SELECT '351806035647381',451 UNION ALL SELECT '352045037654321',207

--- following query returns all records(10)
select * from #data Order By SSN_Number

--- following query gives the distinct TAC's+fiModel, but TACs can repeat (9)
select substring(ssn_number,1,8)as TAC,fiModel,count(*) from #data
group by substring(ssn_number,1,8),fiModel
Order By substring(ssn_number,1,8),fiModel

--- following query gives the correct(distinct) TAC's (4), 
--- but i need the fiModel that occurs most often with the assigned TAC
--- if the number is the same, it doesn't matter what to take
select substring(ssn_number,1,8)as TAC,count(*) from #data
group by substring(ssn_number,1,8)
Order By substring(ssn_number,1,8)

drop table #data

So this is the desired result:

TAC         fiModel
35180603    451
35204503    279
35379702    438
35837201    318

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

人心善变 2024-12-27 02:04:17

这应该可以解决问题(CTE 来救援!):

;with cte as (
    select substring(ssn_number,1,8) as TAC, fiModel, ROW_NUMBER() OVER (PARTITION BY substring(ssn_number,1,8) ORDER BY count(*) desc) as row
    from #data
    group by substring(ssn_number,1,8),fiModel
)
select TAC, fiModel
from cte
where row = 1

作为子查询:

Select TAC,fiModel
from(
    Select substring(ssn_number,1,8)as TAC, fiModel
      ,ROW_NUMBER() OVER (PARTITION BY substring(ssn_number,1,8) ORDER BY count(*) desc) as row
    from #data
    group by substring(ssn_number,1,8),fiModel
)as data
where row=1

This should do the trick (CTE's to the rescue!):

;with cte as (
    select substring(ssn_number,1,8) as TAC, fiModel, ROW_NUMBER() OVER (PARTITION BY substring(ssn_number,1,8) ORDER BY count(*) desc) as row
    from #data
    group by substring(ssn_number,1,8),fiModel
)
select TAC, fiModel
from cte
where row = 1

As subquery:

Select TAC,fiModel
from(
    Select substring(ssn_number,1,8)as TAC, fiModel
      ,ROW_NUMBER() OVER (PARTITION BY substring(ssn_number,1,8) ORDER BY count(*) desc) as row
    from #data
    group by substring(ssn_number,1,8),fiModel
)as data
where row=1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文