棘手的 SQL 问题
我有一个像这样的表...
Key Seq Val
A 1 123
A 4 129
A 9 123
A 10 105
B 3 100
B 6 101
B 12 102
我想找到值(在本例中为 123)之前(在本例中为 A,1)和之后(在本例中为 A,9)相同的情况(如 A,4) )。 seq严格增加,但可能有间隙。 有什么建议吗?
I have a table like this ...
Key Seq Val
A 1 123
A 4 129
A 9 123
A 10 105
B 3 100
B 6 101
B 12 102
I want to find cases (like A,4) where the value(in this case 123) is the same before (in this case A,1) and after (in this case A,9).
The seq increases strictly, but may have gaps.
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
虽然我只在 sql server 2005 中对此进行了测试(因为我没有 2000 实例),但在用真实的表替换 @t 后,这应该仍然可以在该平台上工作。
如果您在
k, seq
上有一个索引,那么性能应该不会太差,因为相关子查询是简单的索引扫描。遗憾的是,我认为 SQL Server 路线图上不支持
lag
和lead
函数。[如果有人感兴趣,我的意思是在某些数据库中你可以写:
如果你想查看前两个或更多值,这肯定会发挥作用,因为你可以写
lag(val, 2)< /code> 向后查看 2 行等。查找紧邻的上一个或下一个值是一个更简单的情况,
select top 1 ...
可以很好地处理。 ]Although I have only tested this in sql server 2005 (since I don't have a 2000 instance around), after replacing @t with a real table this should still work on that platform.
Provided you have an index on
k, seq
the performance shouldn't be too bad, as the correlated subqueries are simple index scans.Sadly, I don't think support for the
lag
andlead
functions is on the SQL Server roadmap.[ In case anyone's interested, I mean that in some databases you can write:
This would definitely come into its own if you wanted to look at the previous two or more values, because you can write
lag(val, 2)
to look 2 rows back etc. Finding the immediately previous or next value is a simpler case thatselect top 1 ...
handles quite nicely. ]我不希望这会跨越数千行:
基本上,表的前 3 个实例将表本身连接起来,根据您的定义找到围绕“有趣”行的两行。后续连接(t4 和 t5)确保 t2 和 t3 搜索找到的行最接近 t1 行。
I wouldn't expect this to blaze through thousands of rows:
Basically, the first 3 instances of the table joins the table on itself to find two rows which surround an "interesting" row, per your definition. The subsequent joins (t4 and t5) ensure that the rows found by the t2 and t3 searches are the closest to the t1 row.
编辑:我在你说 SQL Server 2000 之前写了这篇文章。这适用于 SQL Server 2005 或更高版本,所以它不会帮助你,但我将把它留在这里供后代使用:)
我正在使用 CTE 向表中添加顺序(不间断)排序,然后连接两次以获取上一个和接下来的几行。
Edit: I wrote this before you said SQL Server 2000. This works in SQL Server 2005 or later, so it won't help you, but I'll leave it here for posterity :)
I'm using a CTE to add a sequential (unbroken) ordering to the table, then joining twice to get previous and next rows.
如果您不需要 seq 字段,则此方法有效:
如果您这样做:
编辑:一种不会为您提供 seq 的 tmptbl 方法:
根据字段的类型,这可能很容易更改为给您 seq出色地。
This works if you don't need the seq field:
this if you do:
EDIT: A tmptbl approach that doesn't give you the seq:
Depending on the types of your fields this may be easy to change to give you the seq as well.
假设表的名称是“Table”,这里是普通的 sql。
Assuming the name of the table is "Table" here is the plain vanilla sql.