如何选择SQL中给定组的第一个和第二个记录?

发布于 2025-01-28 07:22:16 字数 3097 浏览 2 评论 0原文

在Teradata中,我需要选择一个组的第一个记录,以及与其他设置条件的多个组的同一组的第二个记录。我该如何获得这个?

例如

ID记录日期1
12022-01-121
ONE22022-01-121
ONE32022-01-121
ONE42022-01-121
ONE12022-01-122
2022-01-121
22022-01-121
两个32022-01-121
TWE2022-01-121
4TAT
52022-01-121
TWE​05-121
两个62022-05-121

的输出

ID记录日期位置
112022-01-121
ONE32022-01-121
TWET1TWE222-01-12TWE
所需组 121

In Teradata I need to select the first record for a group as well as the second to last record for the same group for multiple groups with other set conditions. How can I acheive this?

ex table:

group idrecordsdateplace
One12022-01-121
One22022-01-121
One32022-01-121
One42022-01-121
One12022-01-122
Two12022-01-121
Two22022-01-121
Two32022-01-121
Two42022-01-121
Two52022-01-121
Two62022-01-121
Two52022-05-121
Two62022-05-121

Desired Output:

group idrecordsdateplace
One12022-01-121
One32022-01-121
Two12022-01-121
Two52022-01-121

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

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

发布评论

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

评论(4

会发光的星星闪亮亮i 2025-02-04 07:22:16

我会做这样的事情:

select
*
from
table
qualify row_number() over (partition by groupid order by date ASC) = 1 --"first"
or  row_number() over (partition by groupid order by date DESC) = 2 -- "second to last"

I would do something like this:

select
*
from
table
qualify row_number() over (partition by groupid order by date ASC) = 1 --"first"
or  row_number() over (partition by groupid order by date DESC) = 2 -- "second to last"
最舍不得你 2025-02-04 07:22:16

没有测试,只是一个主意。

select q.*
from
(
select t.*,
max(t.records)-1 over (partition by t.group_id) as mxprev
from yourtable as t
) as q
where q.records=1 or q.records=q.mxprev

Not tested, just an idea.

select q.*
from
(
select t.*,
max(t.records)-1 over (partition by t.group_id) as mxprev
from yourtable as t
) as q
where q.records=1 or q.records=q.mxprev
窗影残 2025-02-04 07:22:16

如果您可以手动指定每个组,则可以使用:(

(SELECT * FROM extable 
WHERE groupid = 'One'
ORDER BY date ASC -- or whatever you want to order by to get "first" and "second to last" 
LIMIT 1) 

UNION 

(SELECT * FROM extable 
WHERE groupid = 'One'
ORDER BY date DESC -- or whatever you want to order by to get "first" and "second to last"
LIMIT 1 
OFFSET 1)

UNION 

(SELECT * FROM extable 
WHERE groupid = 'Two'
ORDER BY date ASC -- or whatever you want to order by to get "first" and "second to last" 
LIMIT 1) 

UNION 

(SELECT * FROM extable 
WHERE groupid = 'Two'
ORDER BY date DESC -- or whatever you want to order by to get "first" and "second to last"
LIMIT 1 
OFFSET 1);

查看一个更通用的解决方案ATM)

This works if you're ok with specifying each group manually:

(SELECT * FROM extable 
WHERE groupid = 'One'
ORDER BY date ASC -- or whatever you want to order by to get "first" and "second to last" 
LIMIT 1) 

UNION 

(SELECT * FROM extable 
WHERE groupid = 'One'
ORDER BY date DESC -- or whatever you want to order by to get "first" and "second to last"
LIMIT 1 
OFFSET 1)

UNION 

(SELECT * FROM extable 
WHERE groupid = 'Two'
ORDER BY date ASC -- or whatever you want to order by to get "first" and "second to last" 
LIMIT 1) 

UNION 

(SELECT * FROM extable 
WHERE groupid = 'Two'
ORDER BY date DESC -- or whatever you want to order by to get "first" and "second to last"
LIMIT 1 
OFFSET 1);

(looking into a more generic solution atm)

娇妻 2025-02-04 07:22:16

应该工作,不测试

select * from 
  (select *
   ,row_number() over(partition by group id order by records) rn1
   from table1
  ) t1 where t1 = 1
union all
select * from 
(
   select *
    ,row_number() over(partition by group id order by records desc) rn2
   from table1
) t2 where  rn2 = 2

Should work , not tested

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