
[英]How to split column based on the min and max value of another column in postgresql
[英]How to update a column based on another table value for max and min sequence
我有以下结构的2个表。 现在,我想生成一个与表t1相同的报告,其中备注列的值为“匹配”或“不匹配”。 选择备注列的条件是,当t2中存在每个Ringid的t1表的最大和最小值SequenceNo列NodeName值时,则所有匹配的备注列值都匹配,否则每个环ID集都不匹配。
表:t1
RingID SequenceNo NodeName ISDName Remarks
1 1 n1 gx1
1 2 n2 gx2
1 3 n3 gx1
1 4 n4 gx3
2 1 n6 gx1
2 2 n7 gx3
2 3 n8 gx5
2 4 n9 gx6
2 5 n10 gh6
表:t2
ID NodeName
1 n1
2 n4
3 n6
4 n7
现在仅用于匹配的环,因为t2表中的seq no 1和4th值
使用函数first_value
和case ... when ...
子句:
select t1.ringid, sequenceno, nodename, isdname,
case when exists (select 1 from table2 where nodename=t1.mn)
and exists (select 1 from table2 where nodename=t1.mx)
then 'matched' else 'not matched'
end remarks
from
(select table1.*,
first_value(nodename) over (partition by ringid order by sequenceno) mn,
first_value(nodename) over (partition by ringid order by sequenceno desc) mx
from table1) t1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.