【问题标题】:Select a range of list from two string value从两个字符串值中选择一个列表范围
【发布时间】:2016-04-06 09:52:31
【问题描述】:

我最近在sql中遇到一个问题,

我有一个名为 Plan 的表格,如下所示,按距离排序...

 Distance Destination
 0         a
 1        null
 2        null
 3         b
 .        null
 .        null
 1050      c

我有两个值作为用户的输入,例如 a 和 b 或 b 和 c ,我怎样才能使用 SQL 获得这个值范围

我所做的是选择 a 和 b 的距离,然后通过执行 distance > a.distance 和 distance

返回一个列表

 0         b
 .        null
 .        null
 1047      c

【问题讨论】:

  • 像样例数据你也可以发布预期数据吗?
  • 显示您想要的样本数据结果,至少对我来说还不清楚。两个目的地之间的距离总和?

标签: sql sql-server


【解决方案1】:

使用withSQL语句

    WITH agreater AS (
        SELECT 
            * 
        FROM 
            Plan 
        WHERE 
            distance >= (select distance from Plan where destination = 'a')
    )
    SELECT 
        * 
    FROM 
        agreater
    WHERE 
        distance <= (select distance from agreater where destination = 'c')

您可以查看this answer 以获取有关with 的更多信息

【讨论】:

    猜你喜欢
    • 2019-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-03
    • 1970-01-01
    • 2016-05-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多