简体   繁体   English

SQL 用于在给定条件下选择第二行

[英]SQL for selecting second row on given condition

A table in snowflake.雪花中的一张桌子。 We have few ref_id's.我们的 ref_id 很少。 Each ref_id is associated with n no.of ord_id with corresponding status.每个 ref_id 都与具有相应状态的 n 个 ord_id 相关联。

ref_id ref_id ord_id ord_id status地位
R1 R1 03 03 Close
R1 R1 01 01 Active积极的
R1 R1 02 02 Active积极的
R2 R2 07 07 null null
R2 R2 04 04 Close
R2 R2 05 05 Active积极的
R3 R3 08 08 Close
R3 R3 09 09 null null

I want to have a new column latest_status which will have the status of highest ord_id for each ref_id.我想要一个新的列 latest_status,它将具有每个 ref_id 的最高 ord_id 的状态。 One more condition is that if the status is null for highest ord_id and if its second highest ord_id status is Active then latest should have active status.另一个条件是,如果最高 ord_id 的状态是 null 并且如果它的第二高 ord_id 状态是活动的,那么最新的应该具有活动状态。

Expected result:预期结果:

ref_id ref_id ord_id ord_id status地位 latest_status最新状态
R1 R1 03 03 Close Close
R1 R1 02 02 Active积极的 Close
R1 R1 01 01 Active积极的 Close
R2 R2 07 07 null null Active积极的
R2 R2 05 05 Active积极的 Active积极的
R2 R2 04 04 Close Active积极的
R3 R3 09 09 null null null null
R3 R3 08 08 Close null null

Can someone please help me with the sql for this?有人可以帮我解决这个问题吗? I have tried below sql but it is failing for the R2 red_id result.我在 sql 下尝试过,但 R2 red_id 结果失败。

select distinct ord_id,status, IFF(status is NULL,NTH_VALUE(IFF(status IN ('Active','Renew'),status,null),2)OVER(PARTITION BY ref_id ORDER BY ord_id desc), FIRST_VALUE(status) OVER(PARTITION BY ref_id ORDER BY ord_id DESC))as latest_status from table where ref_id='R2' select distinct ord_id,status, IFF(status is NULL,NTH_VALUE(IFF(status IN ('Active','Renew'),status,null),2)OVER(PARTITION BY ref_id_statusc), OVER(PARTITION BY ref_id ORDER BY ord_id DESC)) as latest_status from table where ref_id='R2'

Not fully tested and @isolated's question will be something to bear in mind, something like so.没有经过全面测试,@isolated 的问题需要牢记,就像这样。 I've used a table var @t which will be your source.我使用了一个表 var @t,这将是您的来源。 You dont say what DBMS you are using either, so there will be some short-cuts possible i imagine.你也没有说你正在使用什么 DBMS,所以我想可能会有一些捷径。

select * ,
(select sts from @t as t3 where t3.ref=t1.ref and t3.ord=
(select max(ord) from @t as t2 where t2.ref=t1.ref) ) as LATEST
from 
@t as t1

I could get it by doing below我可以通过以下方式得到它

select distinct ord_id,status, FIRST_VALUE(IFF(status is NULL, IFF(B.next IN('Active','Renew'),next,null),status))OVER(PARTITION BY A.ref_id ORDER BY ord_id DESC)as latest from table A LEFT JOIN (select distinct ref_id,FIRST_VALUE(status)OVER(PARTITION BY ref_id ORDER BY ord_id desc) as next from table where status is not null)B on A.ref_id = B.ref_id; select distinct ord_id,status, FIRST_VALUE(IFF(status is NULL, IFF(B.next IN('Active','Renew'),next,null),status))OVER(PARTITION BY A.ref_id)从表 A LEFT JOIN (select distinct ref_id,FIRST_VALUE(status)OVER(PARTITION BY ref_id ORDER BY ord_id desc) 作为状态不为空的表中的下一个)B on A.ref_id = B.ref_id;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM