gpt4 book ai didi

c# - 选择其中 column1 = 1 AND column2 = MAX(column2)

转载 作者:行者123 更新时间:2023-11-30 21:43:58 25 4
gpt4 key购买 nike

我有这样的 table

|Column 1 |Column 2|Column 3|
| 1| 1| 1|
| 2| 1| 2|
| 3| 1| 3|
| 4| 2| 1|
| 5| 1| 4|
| 6| 2| 2|
| 7| 2| 3|
| 8| 2| 4|
| 9| 2| 5|

现在我想要做的是选择 Column 1, Column 2, Column 3 WHERE Column2 = 1 AND Column 3 is largest for column 2 (4)

最佳答案

您可以使用窗口函数 rank 找到 col3 的最大值

select col1, col2, col3 from
(select
col1, col2, col3,
rank() over (order by col3 desc nulls last) rnk
from my_table
where col2 = 1)
where rnk = 1;

或者这样做,如果不支持,但要小心,如果 nulls 存在于 col3 中,您必须处理:

select col1, col2, col3
from my_table t
where col2 = 1
and col3 = (select max(col3)
from my_table
where col2 = t.col2);

关于c# - 选择其中 column1 = 1 AND column2 = MAX(column2),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41171742/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com