我怎样才能在 SQL 中做到这一点?我需要找到一个小数据表的可能排列
我有这个小数据表。
Dir LinkL LinkH
East 19 27
East 27 29
East 29 31
West 46 49
West 49 51
West 51 61
这些代表了可能的行程。我该如何查询这个?例如,如果您从 19 站出发,您可以从 19->27、19->29 和 19->31 出发。三种可能的“行程” 但从27开始,你只有两种可能的“行程”,27->29> 27→31,最后从29开始仅给出一种可能的行程29→31。那是向东行驶,同样的情况向西行驶,您可以有 3 种可能的“行程”,从 46 站出发,46->49,46->51,>51。 46->61,依此类推...从 6 个输入生成 12 个行程记录。我如何编写类似的查询?我从这样的东西开始
SELECT t1.Dir, t1.LinkL 从 tblaa t1 内连接 tblaa t2 打开 t1.Dir = t2.Dir AND t2.LinkL> t1.LinkL
,但我的 Sql 技能有些有限。
所需的输出将类似于以下内容
East 19 27
East 19 29
East 19 31
East 27 29
East 27 31
East 29 31
West 46 49
West 46 51
West 46 61
West 49 51
West 49 61
West 51 61
Making for 12 possible “trips” 有没有人可以指出我正确的方向?我不介意两个单独的查询并将结果合并在一起。如果方向使事情变得更加困难。我还试图避免可怕的光标。
感谢您的任何帮助。
干杯,
〜ck
I have this small table of data.
Dir LinkL LinkH
East 19 27
East 27 29
East 29 31
West 46 49
West 49 51
West 51 61
These represent possible trips. How can I query this? For instance if you started at station 19, you could go from 19->27, 19->29, and 19->31. Three possible "trips" But starting from 27, you only have Two possible "trips", 27->29 & 27->31, lastly starting from 29 gives only one possible trip 29->31. Thats going East, with the same scenario going West you could have 3 possible "trips" starting from station 46, 46->49, 46->51, & 46->61, and so on... Making 12 trip records from the 6 inputs. How do I write a query for something like that? I started with something like this
SELECT t1.Dir, t1.LinkL
FROM tblA t1 INNER JOIN tblA t2
ON t1.Dir = t2.Dir
AND t2.LinkL > t1.LinkL
, but my Sql skills are somewhat limited.
The desired output would be something like this
East 19 27
East 19 29
East 19 31
East 27 29
East 27 31
East 29 31
West 46 49
West 46 51
West 46 61
West 49 51
West 49 61
West 51 61
Making for 12 possible "trips"
Is there anyone out there that can point me in the right direction? I don't mind two separate queries and union the results together. In case the direction makes it more difficult. I'm also trying to avoid the dreaded cursor.
Thanks for any help.
Cheers,
~ck
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这有效:
This works:
尝试:
ON 子句不必基于平等...
Try:
ON clauses do not have to be based on equalities...