以你的数据
广州出发
120元钱
select a.*
from tablename a,(
select max(站号) as 站号 from tablename b
where (select sum(价格) from tablename where 站号<=b.站号)<=120
) c
where a.站号=c.站号
广州出发
120元钱
select a.*
from tablename a,(
select max(站号) as 站号 from tablename b
where (select sum(价格) from tablename where 站号<=b.站号)<=120
) c
where a.站号=c.站号
select top 1 * from tablename b
where (select sum(价格) from tablename where 站号<=b.站号)<=120
order by b.站号 desc
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)*/
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)*/
站) order by 价格 desc
还没看明白你的table_a,table_b,table_c是从哪来的to:mengmou()mengmou()
只能一条SQL实现,不建立临时表的to:ayzwd(菜鸟老三)
价格是指相邻两站之间的价格,不是从起点站到该站的价格,起点站到该站的价格还要求合的
不好意思,前面没看懂你的程序,我测试一下看看
好像也不对哦
唉,这样的东西有什么实际的用处呀?性能低下,作用不大。
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/* 上面的代码是向前的,谁来续个向后的,道理是一样的*/
一个语句--测试环境
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 行)
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 行)
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 行)
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)*/
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
----------------lz放弃吧,效益低的会让你无法忍受
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
--测试结果