下面是我的订单表。起始编号:300,最后编号:900(由客户根据订单提供)
declare @Order table
(
Id int,
[From] int,
[To] int
)
Insert Into @Order
Values
(1,100,400),
(2,401,600),
(3,601,1000),
(4,1001,2000),
Declare @first int=300, @last int=900
必需的输出:
请提供解决方案。我必须根据开始和结束编号计算开始和结束列
答案 0 :(得分:1)
编辑-重新考虑
示例
Declare @Order table( Id int,[From] int,[To] int )
Insert Into @Order Values
(1,100,400),
(2,401,600),
(3,601,1000),
(4,1001,2000)
Declare @first int=300, @last int=900
Select A.*
,[Start] = case when [From]>@First then [From] else @First end
,[End] = case when @Last <[To] then @Last else [To] end
From @Order A
Where [To] between @First and @Last
or @Last between [From] and [To]
返回
Id From To Start End
1 100 400 300 400
2 401 600 401 600
3 601 1000 601 900