在SQL查询中获得超过1的其他等级值
当我在以下使用之类的地方使用时,有1个SQL查询 -
select * from
(select a.id, a.nm, b.pd, b.date, rank() over(partition by a.id order by b.date desc) rnk
from tab1 a, tab2 b where a.id = b.id)
where rnk =1
然后在下面的输出中获得输出,
id nm pd date rnk
-- --- --- ---------- ---
11 abc a15 11/04/2022 1
11 abc a15 11/04/2022 1
14 gef a23 11/04/2022 1
14 gef a23 10/04/2022 12
19 lfb a37 11/04/2022 1
19 lfb a37 08/04/2022 21
但是我只想在最新日期之前选择一个值作为订单。您能帮我解决这个问题,以选择一个值的一个值,例如以下 -
id nm pd date rnk
-- --- --- ---------- ---
11 abc a15 11/04/2022 1
14 gef a23 11/04/2022 1
19 lfb a37 11/04/2022 1
There is 1 SQL query when i used like below-
select * from
(select a.id, a.nm, b.pd, b.date, rank() over(partition by a.id order by b.date desc) rnk
from tab1 a, tab2 b where a.id = b.id)
where rnk =1
then getting output like below-
id nm pd date rnk
-- --- --- ---------- ---
11 abc a15 11/04/2022 1
11 abc a15 11/04/2022 1
14 gef a23 11/04/2022 1
14 gef a23 10/04/2022 12
19 lfb a37 11/04/2022 1
19 lfb a37 08/04/2022 21
But I want only one value to be select as order by latest date. Could you please help me on this to resolve it to select one value for 1 id like below-
id nm pd date rnk
-- --- --- ---------- ---
11 abc a15 11/04/2022 1
14 gef a23 11/04/2022 1
19 lfb a37 11/04/2022 1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要按
RANK()
的顺序指定第二列,以便不存在重复的值对(例如b.id
)。我还规范了JOIN
操作。You need to specify a second column in the order by of the
RANK()
, so that there are no duplicate pairs of values (e.g.b.id
). I've also normalised theJOIN
operation.您是否尝试过这样的尝试,因为有时
rank()
函数不仅在主SQL之外工作。尝试一下,希望它能起作用。Have you tried like this because sometimes
rank()
function doesn't work just outside main SQL. Try it, hope it will work.