Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从sql server中的表中获取前n行_Sql_Sql Server - Fatal编程技术网

从sql server中的表中获取前n行

从sql server中的表中获取前n行,sql,sql-server,Sql,Sql Server,所以让我先粘贴这两个表模式 CREATE TABLE Segment_Master ( [segment_id] bigint, [subject_code_id] bigint, [segment_name] nvarchar(60), [segment_description] nvarchar(250), [must_attend_question] tinyint, [total_question] tinyint, [branch_id] bigint, [entry_by] bigi

所以让我先粘贴这两个表模式

CREATE TABLE Segment_Master ( [segment_id] bigint, [subject_code_id] bigint, [segment_name] nvarchar(60), [segment_description] nvarchar(250), [must_attend_question] tinyint, [total_question] tinyint, [branch_id] bigint, [entry_by] bigint, [entry_date] datetime, [test_id] int, [neg_marks_each_quest] decimal(4,2) )
INSERT INTO Segment_Master
VALUES
( 1, 1, N'First Segment', N'First Segment Description', 5, 5, 15, 10238, N'2018-05-16T13:03:17.583', 1, 0.50 ), 
( 2, 1, N'Second Section', N'Second Segment', 5, 6, 15, 10238, N'2018-05-16T13:03:17.583', 1, 0.00 ), 
( 3, 1, N'Third Segment', N'Third Segment', 1, 2, 15, 10238, N'2018-05-16T13:03:17.583', 1, 0.00 )
现在让我展示第二张桌子

CREATE TABLE OnlineTestAnswer ( [auto_id] bigint, [segment_id] int, [question_id] int, [marks] decimal(9,4), [student_id] int, [test_id] int, [branch_id] int, [faculty_id] int )
INSERT INTO OnlineTestAnswer
VALUES
( 1, 1, 1, 1.0000, 10246, 1, 15, 10246 ), 
( 2, 1, 31, -0.5000, 10246, 1, 15, 10246 ), 
( 3, 1, 32, -0.5000, 10246, 1, 15, 10246 ), 
( 4, 1, 33, -0.5000, 10246, 1, 15, 10246 ), 
( 5, 1, 34, 0.3700, 10246, 1, 15, 10246 ), 
( 6, 2, 2, 0.0000, 10246, 1, 15, 10246 ), 
( 7, 2, 8, 2.0000, 10246, 1, 15, 10246 ), 
( 8, 2, 31, 0.0000, 10246, 1, 15, 10246 ), 
( 9, 2, 35, 1.0000, 10246, 1, 15, 10246 ), 
( 10, 2, 21, 2.0000, 10246, 1, 15, 10246 ), 
( 11, 2, 22, 2.0000, 10246, 1, 15, 10246 ), 
( 12, 3, 15, 3.5000, 10246, 1, 15, 10246 )
现在,如果我在这些表上运行select查询

现在,如果你能在没有放大镜的情况下看到:D,有两列: 必须出席问题和总问题,顾名思义,必须出席问题您可以在按分数排序时使用交叉申请动态获取前N名,然后按每个分段分组

SELECT
    T.segment_id,
    SumTopMarks = SUM(S.marks)
FROM
    Segment_Master AS T
    CROSS APPLY (
        SELECT TOP (T.must_attend_question)
            O.*
        FROM
            OnlineTestAnswer AS O
        WHERE
            T.segment_id = O.segment_id
        ORDER BY
            O.marks DESC
    ) AS S
GROUP BY
    T.segment_id

如果您有一个没有答案的段母版,并且希望将其列出,则可以将交叉应用更改为外部应用。

使用行号和内部查询的解决方案


谢谢您的帮助:
SELECT
    T.segment_id,
    SumTopMarks = SUM(S.marks)
FROM
    Segment_Master AS T
    CROSS APPLY (
        SELECT TOP (T.must_attend_question)
            O.*
        FROM
            OnlineTestAnswer AS O
        WHERE
            T.segment_id = O.segment_id
        ORDER BY
            O.marks DESC
    ) AS S
GROUP BY
    T.segment_id
select O.segment_id,
total=Sum(O.marks)
from 
(
select segment_id,marks,
rn=row_number() over(partition by segment_id order by marks desc)
from OnlineTestAnswer) O
join Segment_Master M
on M.segment_id=O.segment_id and O.rn<=M.must_attend_question
group by O.segment_id