gpt4 book ai didi

比较行的 SQL 查询

转载 作者:行者123 更新时间:2023-12-02 22:31:26 26 4
gpt4 key购买 nike

我有一个这样的表:

    Sequence    ID      Date          other fields

1 23 2012-01-01
2 23 2012-02-03
3 23 2012-02-02
4 45 2012-01-01
5 45 2012-01-02
6 52 2012-01-01
7 52 2012-03-01
.. ...

我需要一个查询来返回具有倒置日期的行(具有较早日期的更高序列

在示例 abow 中,它应该返回带有序列 2 和 3 的 rowq。对于 id 23,Seq 2 的日期比序列号 3 更新。

谢谢

最佳答案

select t1.*
from <table> t1
join <table> t2
on t1.Id=t2.Id
and (((t1.Sequence>t2.Sequence) and (t1.Date<t2.Date)) or
((t1.Sequence<t2.Sequence) and (t1.Date>t2.Date)))

还有它的 fiddle http://sqlfiddle.com/#!3/beaf8/2/0

关于比较行的 SQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12191568/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com