以你的数据
广州出发
120元钱
select a.*
from tablename a,(
select max(站号) as 站号 from tablename b
where (select sum(价格) from tablename where 站号<=b.站号)<=120
) c
where a.站号=c.站号

解决方案 »

  1.   

    or:
    select top 1 * from tablename b
    where (select sum(价格) from tablename where 站号<=b.站号)<=120
    order by b.站号 desc
      

  2.   

    --建立测试环境
    create table #tb(站号 int,价格 int,站名 varchar(10))
    insert #tb(站号,价格,站名)
    select '1','0','广州' union all
    select '2','50','长沙' union all
    select '3','30','武汉' union all
    select '4','220','北京'
    go
    --执行测试语句
    declare @a int set @a = 100--100块
    declare @id int set @id = 2--从长沙开始select top 1 站号
    from #tb t 
    where 站号 >= 2 and (select sum(价格) from #tb where 站号 between @id and t.站号) <=120
    order by 站号 desc
    go
    --删除测试环境
    drop table #tb
    go
    /*--测试结果
    站号          
    ----------- 
    3(1 row(s) affected)*/
      

  3.   

    改改--建立测试环境
    create table #tb(站号 int,价格 int,站名 varchar(10))
    insert #tb(站号,价格,站名)
    select '1','0','广州' union all
    select '2','50','长沙' union all
    select '3','30','武汉' union all
    select '4','220','北京'
    go
    --执行测试语句
    declare @a int set @a = 100--100块
    declare @id int set @id = 2--从长沙开始select top 1 站号
    from #tb t 
    where 站号 >= 2 and (select sum(价格) from #tb where 站号>@id and 站号=t.站号) <=120
    order by 站号 desc
    go
    --删除测试环境
    drop table #tb
    go
    /*--测试结果
    站号          
    ----------- 
    3(1 row(s) affected)*/
      

  4.   

    上面写的都太麻烦,俺给写一个简单一点儿的。select top 1 * from table1 where 价格<=(select 价格+钱数 from table1 where 站点=起点
    站) order by 价格 desc
      

  5.   

    to:Haiwer(海阔天空)
    还没看明白你的table_a,table_b,table_c是从哪来的to:mengmou()mengmou() 
    只能一条SQL实现,不建立临时表的to:ayzwd(菜鸟老三)
    价格是指相邻两站之间的价格,不是从起点站到该站的价格,起点站到该站的价格还要求合的
      

  6.   

    to :mengmou()mengmou()
    不好意思,前面没看懂你的程序,我测试一下看看
      

  7.   

    楼上的不对。我想是实现不了的,就算用C++,也要用递归吧,一条SQL怎么可能。
      

  8.   

    to :mengmou()mengmou()
    好像也不对哦
      

  9.   

    好了,答案想出来了,一条SQL语句搞定,明天测试正确之后贴出答案。
      

  10.   

    select top 1 * from table1 a where (select sum(价格) from table1 where 站点>起点站 and 站点<=a.站点)<=钱数 order by 站点 desc
    唉,这样的东西有什么实际的用处呀?性能低下,作用不大。
      

  11.   

    select top 1 * from table1 a where (select sum(价格) from table1 where 站点>起点站 and 站点<=a.站点)<=钱数 and 站点>起点站 order by 站点 desc
      

  12.   

    /* 数据经过严谨测试,全部通过 */declare @Test table (站号 int, 价格 int, 站名 varchar(4))
    insert @Test
    select 1, 0, '广州' union all
    select 2, 50, '长沙' union all
    select 3, 30, '武汉' union all
    select 4, 220, '北京'-- 极限测试:长沙到北京250,249都不行,电脑实在是没人情味,大家不要怪它,因为它根本不是人!
    declare @Money int, @StartStation varchar(4)
    select @Money = 249, @StartStation = '长沙'-- 为了代码简洁性,还要定义两个变量:
    declare @StartNo int, @PriceDifference int
    select @StartNo = 站号 from @Test where 站名 = @StartStation
    select @PriceDifference = sum(价格) from @Test where 站号 <= @StartNo
    -- 等等,楼主要求用一条SQL语句,现在已经写了N句了——如果不嫌代码长,大可以将这些变量代换回SELECT语句。-- 结果马上出来:
    select TOP 1 站名 from (
    select 站号, 价格 = (select sum(价格) from @Test where 站号 <= m.站号) - @PriceDifference, 站名 from @Test m where 站号 >= @StartNo
    ) a where a.价格 <= @Money order by a.站号 desc/*
    大家可以测试@Money=29的情况——没钱暂时在当地转转吧!
    大家也可以测试@StartStation='北京'的情况——前方没铁路再多的钱也坐不上火车!
    */--这是完全不使用变量的语句:
    select top 1 站名 from (
    select 站号, 价格 = (select sum(价格) from @Test where 站号 <= m.站号) - (select sum(价格) from @Test where 站号 <= (select 站号 from @Test where 站名 = '长沙')), 站名 from @Test m where 站号 >= (select 站号 from @Test where 站名 = '长沙')
    ) a where a.价格 <= 249 order by a.站号 desc/* 上面的代码是向前的,谁来续个向后的,道理是一样的*/
      

  13.   

    考虑两个方向
    一个语句--测试环境
    declare @Test table (站号 int, 价格 int, 站名 varchar(4))
    insert @Test
    select 1, 0, '广州' union all
    select 2, 50, '长沙' union all
    select 3, 30, '武汉' union all
    select 4, 220, '北京'--语句
    select top 1 * from (
    select 站号,站名,(select sum(价格) from @test where 站号<=b.站号 and 站号>(select 站号 from @test where 站名='长沙')) as 价格
    from @test b
    union all
    select 站号,站名,(select sum(价格) from @test where 站号>b.站号 and 站号<=(select 站号 from @test where 站名='长沙')) as 价格
    from @test b
    ) as t
    where 价格<=250
    order by 价格 desc--结果
    站号          站名   价格          
    ----------- ---- ----------- 
    4           北京   250(所影响的行数为 1 行)
      

  14.   

    --换个参数,根据价格自动选择了反向的--测试环境
    declare @Test table (站号 int, 价格 int, 站名 varchar(4))
    insert @Test
    select 1, 0, '广州' union all
    select 2, 50, '长沙' union all
    select 3, 30, '武汉' union all
    select 4, 220, '北京'--语句
    select top 1 * from (
    select 站号,站名,(select sum(价格) from @test where 站号<=b.站号 and 站号>(select 站号 from @test where 站名='长沙')) as 价格
    from @test b
    union all
    select 站号,站名,(select sum(价格) from @test where 站号>b.站号 and 站号<=(select 站号 from @test where 站名='长沙')) as 价格
    from @test b
    ) as t
    where 价格<=249
    order by 价格 desc--结果
    站号          站名   价格          
    ----------- ---- ----------- 
    1           广州   50(所影响的行数为 1 行)
      

  15.   

    好象楼主没定义远的概念,应该可以有两种理解
    1 价格高的远,上面是按这个
    2 站数多的远,可以如下写--测试环境
    declare @Test table (站号 int, 价格 int, 站名 varchar(4))
    insert @Test
    select 1, 0, '广州' union all
    select 2, 50, '长沙' union all
    select 3, 30, '武汉' union all
    select 4, 220, '北京'--语句
    select top 1 * from (
    select 站号,站名,(select sum(价格) from @test where 站号<=b.站号 and 站号>(select 站号 from @test where 站名='长沙')) as 价格
    ,abs(站号-(select 站号 from @test where 站名='长沙')) as 站数
    from @test b
    union all
    select 站号,站名,(select sum(价格) from @test where 站号>b.站号 and 站号<=(select 站号 from @test where 站名='长沙')) as 价格
    ,abs(站号-(select 站号 from @test where 站名='长沙')) as 站数
    from @test b
    ) as t
    where 价格<=249
    order by 站数 desc,价格 desc--结果
    站号          站名   价格          站数          
    ----------- ---- ----------- ----------- 
    1           广州   50          1(所影响的行数为 1 行)
      

  16.   

    --笔误,少写了一个"<"--建立测试环境
    create table #tb(站号 int,价格 int,站名 varchar(10))
    insert #tb(站号,价格,站名)
    select '1','0','广州' union all
    select '2','50','长沙' union all
    select '3','30','武汉' union all
    select '4','220','北京'
    go
    --执行测试语句
    declare @a int set @a = 100--100块
    declare @id int set @id = 2--从长沙开始select top 1 站号
    from #tb t 
    where 站号 >= 2 and (select sum(价格) from #tb where 站号>@id and 站号<=t.站号) <=249
    order by 站号 desc
    go
    --删除测试环境
    drop table #tb
    go
    /*--测试结果
    站号          
    ----------- 
    3(1 row(s) affected)*/
      

  17.   

    这贴建议版主加精,Haiwer(海阔天空)和mengmou()mengmou()太厉害了!!!
      

  18.   

    --建立测试环境
    create table #tb(站号 int,价格 int,站名 varchar(10))
    insert #tb(站号,价格,站名)
    select '1','0','广州' union all
    select '2','50','长沙' union all
    select '3','30','武汉' union all
    select '4','220','北京'
    go--测试 
    select * from #tb a where (select sum(case 站号 when 2 then 0 else 价格 end) 
    from #tb b where b.站号<=a.站号)<=260 and a.站号>=2
      

  19.   

    用一条SQL语句
    ----------------lz放弃吧,效益低的会让你无法忍受
      

  20.   

    --建立测试环境
    create table #tb(站号 int,价格 int,站名 varchar(10))
    insert #tb(站号,价格,站名)
    select '1','0','广州' union all
    select '2','50','长沙' union all
    select '3','30','武汉' union all
    select '4','220','北京'
    gocreate table #fromto(fromNo int, toNo int, moneys int)
    insert #fromto(fromNo,toNo,moneys)
    select '1','2','50' union all
    select '1','3','80' union all
    select '1','4','300' union all
    select '2','3','30' union all
    select '2','4','250' union all
    select '3','4','220'
    go
    --执行测试语句
    declare @a int set @a =79--100块
    declare @id int set @id = 1--从长沙开始
    select max(toNo) as 站 from #fromto where #fromto.fromNo = @id and   @a >= moneysgo
    --删除测试环境
    drop table #tb
    drop table #fromto
    go
    --测试结果
      

  21.   

    感谢Limpire(昨夜小楼) 、Haiwer(海阔天空)、mengmou()mengmou() 三位的答案,为了表示本人的诚意,本人决定给本贴加分结贴,再次感谢三次,并同时感谢其它各位的支持。