MSSQL获得第10 - 20行

时间:2016-04-01 10:18:51

标签: sql-server

您好我想在我的网站项目中为我的数据库中的一个分页器提取行。

SELECT DISTINCT jtg.JobID, jtg.CreatedOn, ksnr.value as Snr, kkz.value 
as Kennz, kvu.value as Vu, kstr.value as Strecke, kkzvn.value as KennzVN, ttr.jobid1 as messageId 
FROM tbl_one jtg 
LEFT JOIN tbl_two ksnr on jtg.jobid=ksnr.jobid and ksnr.keyid=6 
LEFT JOIN tbl_two kkz on jtg.jobid=kkz.jobid and kkz.keyid=29 
LEFT JOIN tbl_two kvu on jtg.jobid=kvu.jobid and kvu.keyid=15 
LEFT JOIN tbl_two kstr on jtg.jobid=kstr.jobid and kstr.keyid=24 
LEFT JOIN tbl_two kkzvn on jtg.jobid=kkzvn.jobid and kkzvn.keyid=30 
LEFT JOIN tbl_three ttr on jtg.jobid=ttr.jobid2 
WHERE jtg.JobID IN (SELECT words.JobId FROM tbl_four words LEFT JOIN tbl_five keys on words.KeyID = keys.KeyID WHERE keys.Name = 'Schadennummer')

这是我的常规查询,工作正常。现在我想获取特定行的onyl,例如10 - 20.

select * from (select row_number() over(order by jtg.jobid) num,* from 
tbl_Job_Tracking_Generator jtg) a 
where num > 10 and num < 21

这个查询很简单,给我10-20行,但我不能将它与上面的主要查询结合起来。我尝试了很多东西,但不能正确。希望你们能帮助我。

2 个答案:

答案 0 :(得分:0)

您需要定义一个列,该列可用于在SQL paging with Row_Number() function

中对数据进行排序

这是一个示例

;with cte as (
SELECT 
    DISTINCT 
    ROW_NUMBER() Over (Order By jtg.JobID Desc) rn,
    jtg.JobID, 
    jtg.CreatedOn, 
    ksnr.value as Snr, 
    kkz.value as Kennz, 
    kvu.value as Vu, 
    kstr.value as Strecke, 
    kkzvn.value as KennzVN, 
    ttr.jobid1 as messageId 
FROM tbl_one jtg 
LEFT JOIN tbl_two ksnr on jtg.jobid=ksnr.jobid and ksnr.keyid=6 
LEFT JOIN tbl_two kkz on jtg.jobid=kkz.jobid and kkz.keyid=29 
LEFT JOIN tbl_two kvu on jtg.jobid=kvu.jobid and kvu.keyid=15 
LEFT JOIN tbl_two kstr on jtg.jobid=kstr.jobid and kstr.keyid=24 
LEFT JOIN tbl_two kkzvn on jtg.jobid=kkzvn.jobid and kkzvn.keyid=30 
LEFT JOIN tbl_three ttr on jtg.jobid=ttr.jobid2 
WHERE 
    jtg.JobID IN (
        SELECT words.JobId 
        FROM tbl_four words 
        LEFT JOIN tbl_five keys on words.KeyID = keys.KeyID 
        WHERE keys.Name = 'Schadennummer'
    )
)
select * from cte where rn between 11 and 20

另一方面,SQL Server 2012在SQL中为数据库开发人员引入了Order By with Offset and Fetch Next for paging

答案 1 :(得分:0)

如果您使用的是SQL 2012,则可以使用offset和fetch ..

with
cte
as
(
SELECT DISTINCT jtg.JobID, jtg.CreatedOn, ksnr.value as Snr, kkz.value 
as Kennz, kvu.value as Vu, kstr.value as Strecke, kkzvn.value as KennzVN, ttr.jobid1 as messageId 
FROM tbl_one jtg 
LEFT JOIN tbl_two ksnr on jtg.jobid=ksnr.jobid and ksnr.keyid=6 
LEFT JOIN tbl_two kkz on jtg.jobid=kkz.jobid and kkz.keyid=29 
LEFT JOIN tbl_two kvu on jtg.jobid=kvu.jobid and kvu.keyid=15 
LEFT JOIN tbl_two kstr on jtg.jobid=kstr.jobid and kstr.keyid=24 
LEFT JOIN tbl_two kkzvn on jtg.jobid=kkzvn.jobid and kkzvn.keyid=30 
LEFT JOIN tbl_three ttr on jtg.jobid=ttr.jobid2 
WHERE jtg.JobID IN (SELECT words.JobId FROM tbl_four words LEFT JOIN tbl_five keys on words.KeyID = keys.KeyID WHERE keys.Name = 'Schadennummer')
)
select * from 
cte
order by jobid,createdon,value--idea here is to make this order by unique
offset 10 rows fetch next 10 rows only