一个选择语句中的两个依赖的聚合函数是可能的吗?
let me show You a simple table at first:
inr | party_id | ver |
---|---|---|
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 |
值'00006693'。我必须从VER列中获取最大值:在这种情况下,这将是“ 0025”。但是,如果列中的所有值都是相同的,则我必须从INR列获得最大值。在这种情况下,这将是“ 00024727”。结果最终应该看起来像:
INR | Party_ID | VER |
---|---|---|
00020222 | 00020107 | 0001 |
00006692 | 00006693 | 0025 |
0025 00006691 | 00006692 | 0018 |
00029732 | 00013671 | 0001 |
我的问题是,如何使用这两个max()我不擅长分析问题。
我还将添加一个图像,其中描述了问题(我认为)更好,请基于它:
let me show You a simple table at first:
inr | party_id | ver |
---|---|---|
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 |
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:
inr | party_id | ver |
---|---|---|
00020222 | 00020107 | 0001 |
00006692 | 00006693 | 0025 |
00006691 | 00006692 | 0018 |
00029732 | 00013671 | 0001 |
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:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我可以提供两个选择。简单的解决方案 - 只需按party_id组中的“ ver desc,inr desc”排序:
第二个解决方案更多详细,但直接实现逻辑:
I can offer two options. Simple solution - just sort by "ver desc, inr desc" within party_id group:
Second solution more verbose, but implements your logic directly:
我认为这在一个选择的语句中不可行,因为窗口函数具有其局限性,但是您需要的详细信息(包括 row_number()订购功能:
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: