我怎样才能在 SQL 中做到这一点?我需要找到一个小数据表的可能排列

发布于 2024-09-06 17:11:30 字数 1000 浏览 4 评论 0原文

我有这个小数据表。

 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

妞丶爷亲个 2024-09-13 17:11:30

这有效:

;WITH trip_cte AS
(
    SELECT
        T1.dir,
        T1.linkl AS start,
        T1.linkh AS finish
    FROM
        dbo.Trips T1
    UNION ALL
    SELECT
        CTE.dir,
        CTE.start,
        T.linkh AS finish
    FROM
        trip_cte CTE
    INNER JOIN dbo.Trips T ON
        T.linkl = CTE.finish
)
SELECT
    dir,
    start,
    finish
FROM
    trip_cte

This works:

;WITH trip_cte AS
(
    SELECT
        T1.dir,
        T1.linkl AS start,
        T1.linkh AS finish
    FROM
        dbo.Trips T1
    UNION ALL
    SELECT
        CTE.dir,
        CTE.start,
        T.linkh AS finish
    FROM
        trip_cte CTE
    INNER JOIN dbo.Trips T ON
        T.linkl = CTE.finish
)
SELECT
    dir,
    start,
    finish
FROM
    trip_cte
江湖正好 2024-09-13 17:11:30

尝试:

SELECT mt1.Dir, mt1.LinkL, mt2.LinkH
 from #MyTable mt1
  inner join #MyTable mt2
   on mt2.Dir = mt1.Dir
    and mt2.LinkH > mt1.LinkL
 order by mt1.Dir, mt1.LinkL, mt2.LinkH

ON 子句不必基于平等...

Try:

SELECT mt1.Dir, mt1.LinkL, mt2.LinkH
 from #MyTable mt1
  inner join #MyTable mt2
   on mt2.Dir = mt1.Dir
    and mt2.LinkH > mt1.LinkL
 order by mt1.Dir, mt1.LinkL, mt2.LinkH

ON clauses do not have to be based on equalities...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文