排序每组大小n

时间:2015-02-09 06:38:52

标签: sql sql-server

我想从N个K个条目中排序每组N个条目

e.g。我们说我们有桌子

Student 
(
   StudentID int, 
   StudentName varchar(50), 
   DepartmentName varchar(50), 
   Marks float
)

并且它有100个条目我希望按照特定标准对每10个条目进行排序,让Marks升序。

e.g。请考虑以下数据

StudentID  StudentName  Departmentname  Marks 
1            A             CS             98.7 
2            B             IT              96.78 
3            C             Civil           95.67 
4            D             Electronics      93.25 

我有网格,它有分页机制。如果我设置pageSize = 2然后在第1页上,数据将是

StudentID  StudentName  Departmentname  Marks 
1            A             CS             98.7 
2            B             IT              96.78 

在第2页上,数据将是

StudentID  StudentName  Departmentname  Marks 
3            C             Civil           95.67 
4            D             Electronics      93.25 

如果我按照StudentName DESC的顺序对网格进行排序,那么整个数据就会被排序,而对于第1页,我的数据将是

 StudentID  StudentName  Departmentname  Marks 
4            D             Electronics      93.25 
3            C             Civil           95.67 

和第2页的数据将是

 StudentID  StudentName  Departmentname  Marks 
2            B             IT              96.78 
1            A             CS             98.7 

而不是它,预期的输出将如第1页所示

StudentID  StudentName  Departmentname  Marks 
2            B             IT              96.78 
1            A             CS             98.7 

和第2页将是

StudentID  StudentName  Departmentname  Marks 
4            D             Electronics      93.25 
3            C             Civil           95.67 

这就是为什么我要在这里专门对大小为2的数据进行排序而不是对整个数据进行排序。

一般情况下,我们总共有100个条目,想要对每10个条目进行排序,然后排序。 13应用排序后,其行号应位于行号中。 11排没有。 20

4 个答案:

答案 0 :(得分:2)

试试这个:

由Marks DESC订购的示例数据

StudentID   StudentName DepartmentName  Marks
----------- ----------- --------------- ----------------------
1           A           CS              98.7
2           B           IT              96.78
3           C           Civil           95.67
4           D           Electronics     93.25

解决方案

DECLARE @pagSize INT = 2
DECLARE @pageNumber INT = 1 --Page you want to sort

;WITH SortByMarks AS( --Sorted first by Marks DESC
    SELECT 
        *, 
        rn = ROW_NUMBER() OVER(ORDER BY Marks DESC),
        pageNumber = (ROW_NUMBER() OVER(ORDER BY Marks DESC) - 1)/@pagSize + 1
    FROM Student
)
SELECT
    StudentId,
    StudentName,
    DepartmentName,
    Marks
FROM SortByMarks
WHERE
    pageNumber = @pageNumber
ORDER BY StudentName DESC

<强> RESULT

StudentId   StudentName DepartmentName  Marks
----------- ----------- --------------- ----------------------
2           B           IT              96.78
1           A           CS              98.7

答案 1 :(得分:0)

如果我理解这个问题......在oracle中,这就是它的发生方式

选择STUDENT_ID(从STUDENT订单中选择*由STUDENT_MARKS asc),其中ROWNUM&lt; = 10 减去 选择STUDENT_ID(从STUDENT订单中选择*由STUDENT_MARKS asc),其中ROWNUM&lt; = 1

这里我们按升序排序标记然后取出除第一行之外的前九行。 如果它不是你想要的,那么再解释一下这个问题

答案 2 :(得分:0)

这应该有效:

- 这是为了对Marks上的每2条记录进行排序

SELECT *
,ROW_NUMBER() OVER (
    PARTITION BY (Row1 + 1) / 2 ORDER BY C2
    ) Row2
FROM (
SELECT *
    ,ROW_NUMBER() OVER (
        ORDER BY C1 DESC
        ) Row1
FROM (
    SELECT 1 AS ID
        ,'A' AS C1
        ,98.7 AS C2

    UNION

    SELECT 2 AS ID
        ,'B' AS C1
        ,96.78 AS C2

    UNION

    SELECT 3 AS ID
        ,'C' AS C1
        ,95.67 AS C2

    UNION

    SELECT 4 AS ID
        ,'D' AS C1
        ,93.25 AS C2
    ) T
) T1;

对于订购10条记录,以下是查询:

SELECT *
,ROW_NUMBER() OVER (
    PARTITION BY (Row1 + 1) / 10 ORDER BY C2
    ) Row2
FROM (
SELECT *
    ,ROW_NUMBER() OVER (
        ORDER BY C1 DESC
        ) Row1
FROM (
    SELECT 1 AS ID
        ,'A' AS C1
        ,98.7 AS C2

    UNION

    SELECT 2 AS ID
        ,'B' AS C1
        ,96.78 AS C2

    UNION

    SELECT 3 AS ID
        ,'C' AS C1
        ,95.67 AS C2

    UNION

    SELECT 4 AS ID
        ,'D' AS C1
        ,93.25 AS C2
    ) T
) T1;

如果这不是你想要的,请告诉我。

答案 3 :(得分:0)

同一个Mahesh, 让我们假设用户点击第2页。 所以现在我对rownum的输入将是2 * 10而对于其他rownum我的输入将是(2-1)* 10,并且根据选择的分页,这将给你下一个10 10数据。

所以你只需要在程序中捕获分页的页码,并将其作为输入。

select STUDENT_ID (select * from STUDENTS order by STUDENT_MARKS asc) where ROWNUM<=[$pageRequest]*10 minus select STUDENT_ID (select * from STUDENTS order by STUDENT_MARKS asc) where ROWNUM<=([$pageRequest] -1)*20