在SQL查询中获得超过1的其他等级值

发布于 2025-01-20 12:10:59 字数 797 浏览 3 评论 0原文

当我在以下使用之类的地方使用时,有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 技术交流群。

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

发布评论

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

评论(2

萌酱 2025-01-27 12:10:59

您需要按RANK() 的顺序指定第二列,以便不存在重复的值对(例如b.id)。我还规范了 JOIN 操作。

select * from
  (select 
     a.id, 
     a.nm, 
     b.pd, 
     b.date, 
     rank() over (
        partition by a.id 
        order by b.[date] desc, b.id asc
        ) as rnk 
  from tab1 a
  join tab2 b on a.id = b.id
  ) s
where rnk = 1; 

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 the JOIN operation.

select * from
  (select 
     a.id, 
     a.nm, 
     b.pd, 
     b.date, 
     rank() over (
        partition by a.id 
        order by b.[date] desc, b.id asc
        ) as rnk 
  from tab1 a
  join tab2 b on a.id = b.id
  ) s
where rnk = 1; 
酷到爆炸 2025-01-27 12:10:59

您是否尝试过这样的尝试,因为有时rank()函数不仅在主SQL之外工作。尝试一下,希望它能起作用。

SELECT id,
       nm,
       pd, date
FROM
  (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

Have you tried like this because sometimes rank() function doesn't work just outside main SQL. Try it, hope it will work.

SELECT id,
       nm,
       pd, date
FROM
  (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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文