选择两列范围之间的值

时间:2016-11-03 12:29:57

标签: sql sql-server sql-server-2014

我有一张这样的表:

i1   i2
----------
1    a
1    b
1    c
1    d
2    x
3    y
4    a
4    b
4    c

我想选择1 c到4 a之间的行。
结果应该是:

1 c
1 d 
2 x 
3 y 
4 a

我该怎么做?

4 个答案:

答案 0 :(得分:5)

如果您使用的是支持行号功能的数据库,那么一个选项是根据您指定的顺序(即i2首先按升序排序)创建包含行号的表的CTE,然后按1c秒。

然后,使用两个子查询来标识4a;WITH cte AS ( SELECT ROW_NUMBER() OVER (ORDER BY i1, i2) AS RowNumber, i1, i2 FROM yourTable ) SELECT * FROM cte t WHERE t.RowNumber >= (SELECT RowNumber FROM cte WHERE i1=1 AND i2='c') AND t.RowNumber <= (SELECT RowNumber FROM cte WHERE i1=4 AND i2='a') 的行号。这些行号构成您要选择的范围。

{{1}}

答案 1 :(得分:5)

我会这样做:

/srv/users/serverpilot/apps/APPNAME/public/

答案 2 :(得分:3)

当然不是最佳解决方案,但此查询应该有效:

controller

$users = DB::table('travel_request')
        ->join('department', 'travel_request.department_id', '=', 'department.id')
        ->join('users', 'travel_request.approver_id', '=', 'users.id')
        ->join('travel_purpose', 'travel_request.travel_purpose_id', '=', 'travel_purpose.id')
        ->join('traveling_details', 'travel_request.id','=','traveling_details.travel_request_id' )
        ->join('country','country.id', '=', 'traveling_details.country_id')
        ->join('travel_type','traveling_details.travel_type_id', '=', 'travel_type.id')
        ->join('travel_request_status','travel_request.status_id', '=', 'travel_request_status.id')
        ->select('travel_request.*', 'travel_request.id as travel_id','department.name as dept_name','users.firstname as approver_name','travel_purpose.purpose','country.name as country_name','traveling_details.from_date','traveling_details.to_date','travel_request_status.status','travel_type.type as travelType')->where('travel_request.id',$id)->get(); 

      Mail::Send('email.welcome', $users, function($message) use($users)  {
        $message->to('sample.demo@domain.com', 'Travel Request')->subject
        ('Testing Mail');
        $message->from('sampele2.sample@demo.com','dummyname');
    });   


 welcome.blade.php
 @foreach ($users as $user)
    {{$user->traveler_name}}
 @endforeach

请注意,(1,c)和(4,a)包含在结果中。如果您不需要包含边框,请更改比较运算符。

答案 3 :(得分:3)

不是那么漂亮的方式......但是

create procedure GetRangeBetween (@i11 int, @i12 char, @i21 int, @i22 char)
AS
BEGIN

if object_id ('tempdb..#Test') is not null drop table #Test

create table #Test (i1 int, i2 nvarchar(10), [Rank] int)

insert into #Test(i1,   i2)
values
(1,   'a'),  (1,    'b'), (1,    'c'),
(1,    'd'), (2,    'x'), (3,    'y'),
(4,    'a'), (4,    'b'), (4,    'c')

update #Test
    set Rank = src.[srcRank]
from #Test t
    join (select *, row_number() over (order by i1) [srcRank] from #Test) src 
        on t.i1 = src.i1 and t.i2 = src.i2

declare @Rank1 int = (select [Rank] from #Test where i1 = @i11 and i2 = @i12)
declare @Rank2 int = (select [Rank] from #Test where i1 = @i21 and i2 = @i22)

select i1, i2 from #Test
    where (i1 between @i11 and @i21) and ([Rank] between @Rank1 and @Rank2) 
END

然后你只需用....执行它

execute GetRangeBetween 1, 'c', 4, 'a'