一个选择语句中的两个依赖的聚合函数是可能的吗?

发布于 2025-01-22 07:12:19 字数 3636 浏览 0 评论 0原文


let me show You a simple table at first:

inrparty_idver
00020222000201070001
00006692000066930025
00021768000066930006
00024726000066930001
00024727000066930000
00006691000066920018
00021949000066920000
00024728000066920000
00024928000066920000
00013670000136710000
00027865000136710000
00029716000136710001
00029732000136710001
00029749000136710000

值'00006693'。我必须从VER列中获取最大值:在这种情况下,这将是“ 0025”。但是,如果列中的所有值都是相同的,则我必须从INR列获得最大值。在这种情况下,这将是“ 00024727”。结果最终应该看起来像:

INRParty_IDVER
00020222000201070001
00006692000066930025
0025 00006691000066920018
00029732000136710001

我的问题是,如何使用这两个max()我不擅长分析问题。

我还将添加一个图像,其中描述了问题(我认为)更好,请基于它:

let me show You a simple table at first:

inrparty_idver
00020222000201070001
00006692000066930025
00021768000066930006
00024726000066930001
00024727000066930000
00006691000066920018
00021949000066920000
00024728000066920000
00024928000066920000
00013670000136710000
00027865000136710000
00029716000136710001
00029732000136710001
00029749000136710000

Look at the value '00006693'. I have to get max value from ver column: this will be '0025' in this case. However, if all values in the column are the same, then I have to get max value from inr column. this will be '00024727' in this case. The result should eventually look like:

inrparty_idver
00020222000201070001
00006692000066930025
00006691000066920018
00029732000136710001

My question is, how to get this data by using two max() functions? I am not good at analytical issues.

I will also add an image, where the problem is described (I think) better and please base on it:

enter image description here

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

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

发布评论

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

评论(2

尛丟丟 2025-01-29 07:12:19

我可以提供两个选择。简单的解决方案 - 只需按party_id组中的“ ver desc,inr desc”排序:

with test_data as (
  select '00020222' inr,    '00020107' party_id,    '0001' ver union
  select '00006692',    '00006693', '0025' union
  select '00021768',    '00006693', '0006' union
  select '00024726',    '00006693', '0001' union
  select '00024727',    '00006693', '0000' union
  select '00006691',    '00006692', '0018' union
  select '00021949',    '00006692', '0000' union
  select '00024728',    '00006692', '0000' union
  select '00034567',    '00019734', '0022' union
  select '00064657',    '00019734', '0022'
)
select
  r.inr, r.party_id, r.ver
from 
(  
  select 
    t.*, 
    row_number() over(partition by t.party_id order by t.ver desc, t.inr desc) rn
  from
    test_data t
) r
where
  r.rn = 1

第二个解决方案更多详细,但直接实现逻辑:

with test_data as (
  select '00020222' inr,    '00020107' party_id,    '0001' ver union
  select '00006692',    '00006693', '0025' union
  select '00021768',    '00006693', '0006' union
  select '00024726',    '00006693', '0001' union
  select '00024727',    '00006693', '0000' union
  select '00006691',    '00006692', '0018' union
  select '00021949',    '00006692', '0000' union
  select '00024728',    '00006692', '0000' union
  select '00034567',    '00019734', '0022' union
  select '00064657',    '00019734', '0022'
)
select 
  r.inr, r.party_id, r.ver
from
(  
  select 
    t.*,
    case when count(distinct t.ver) over(partition by t.party_id) == 1 then 1 else 0 end is_all_ver_same,
    row_number() over(partition by t.party_id order by t.ver desc) max_ver,
    row_number() over(partition by t.party_id order by t.inr desc) max_inr
  from 
    test_data t
) r   
where 
  (r.is_all_ver_same = 1 and r.max_inr = 1) or (r.is_all_ver_same = 0 and r.max_ver = 1)

I can offer two options. Simple solution - just sort by "ver desc, inr desc" within party_id group:

with test_data as (
  select '00020222' inr,    '00020107' party_id,    '0001' ver union
  select '00006692',    '00006693', '0025' union
  select '00021768',    '00006693', '0006' union
  select '00024726',    '00006693', '0001' union
  select '00024727',    '00006693', '0000' union
  select '00006691',    '00006692', '0018' union
  select '00021949',    '00006692', '0000' union
  select '00024728',    '00006692', '0000' union
  select '00034567',    '00019734', '0022' union
  select '00064657',    '00019734', '0022'
)
select
  r.inr, r.party_id, r.ver
from 
(  
  select 
    t.*, 
    row_number() over(partition by t.party_id order by t.ver desc, t.inr desc) rn
  from
    test_data t
) r
where
  r.rn = 1

Second solution more verbose, but implements your logic directly:

with test_data as (
  select '00020222' inr,    '00020107' party_id,    '0001' ver union
  select '00006692',    '00006693', '0025' union
  select '00021768',    '00006693', '0006' union
  select '00024726',    '00006693', '0001' union
  select '00024727',    '00006693', '0000' union
  select '00006691',    '00006692', '0018' union
  select '00021949',    '00006692', '0000' union
  select '00024728',    '00006692', '0000' union
  select '00034567',    '00019734', '0022' union
  select '00064657',    '00019734', '0022'
)
select 
  r.inr, r.party_id, r.ver
from
(  
  select 
    t.*,
    case when count(distinct t.ver) over(partition by t.party_id) == 1 then 1 else 0 end is_all_ver_same,
    row_number() over(partition by t.party_id order by t.ver desc) max_ver,
    row_number() over(partition by t.party_id order by t.inr desc) max_inr
  from 
    test_data t
) r   
where 
  (r.is_all_ver_same = 1 and r.max_inr = 1) or (r.is_all_ver_same = 0 and r.max_ver = 1)
女中豪杰 2025-01-29 07:12:19

我认为这在一个选择的语句中不可行,因为窗口函数具有其局限性,但是您需要的详细信息(包括 row_number()订购功能:

if object_id(N'tempdb..#Temp') is not null 
drop table #Temp
create table #Temp (
    inr int null,
    party_id int null,
    ver int null
)
insert into #Temp
    (
        inr,
        party_id,
        ver
    )
values
    (00020222,  00020107,   0001),
    (00006692,  00006693,   0025),
    (00021768,  00006693,   0006),
    (00024726,  00006693,   0001),
    (00024727,  00006693,   0000),
    (00006691,  00006692,   0018),
    (00021949,  00006692,   0000),
    (00024728,  00006692,   0000),
    (00024928,  00006692,   0000),
    (00013670,  00013671,   0000),
    (00027865,  00013671,   0000),
    (00029716,  00013671,   0001),
    (00029732,  00013671,   0001),
    (00029749,  00013671,   0000)

select 
    inr,
    party_id,
    ver
from
    (
        select
            row_number() over (partition by party_id order by ver desc, inr desc) as rn,
            *
        from #Temp
    ) a
where rn = 1

I think it's not doable in one select statement since window functions has their limitations but what you need can be solved with a subquery including a ROW_NUMBER() function for ordering:

if object_id(N'tempdb..#Temp') is not null 
drop table #Temp
create table #Temp (
    inr int null,
    party_id int null,
    ver int null
)
insert into #Temp
    (
        inr,
        party_id,
        ver
    )
values
    (00020222,  00020107,   0001),
    (00006692,  00006693,   0025),
    (00021768,  00006693,   0006),
    (00024726,  00006693,   0001),
    (00024727,  00006693,   0000),
    (00006691,  00006692,   0018),
    (00021949,  00006692,   0000),
    (00024728,  00006692,   0000),
    (00024928,  00006692,   0000),
    (00013670,  00013671,   0000),
    (00027865,  00013671,   0000),
    (00029716,  00013671,   0001),
    (00029732,  00013671,   0001),
    (00029749,  00013671,   0000)

select 
    inr,
    party_id,
    ver
from
    (
        select
            row_number() over (partition by party_id order by ver desc, inr desc) as rn,
            *
        from #Temp
    ) a
where rn = 1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文