1

如何为按年排名前 25 的学生创建存储过程。所以 Proc 应该将年份作为参数并返回,学生 id,当年的分数总和

Alter  PROCEDURE getstudents
(  
@year INT
)
AS
BEGIN
SELECT student-name FROM tbl_Students WHERE year=@year
Thanks 
4

3 回答 3

2

试试这个——

ALTER PROCEDURE dbo.getstudents

      @year INT

AS
BEGIN

    SELECT TOP 25
            student_name
          , student_id
          , marks = SUM(marks)
    FROM dbo.tbl_Students 
    WHERE [Year] = @year
     GROUP BY 
            student_name
          , student_id
     ORDER BY marks DESC

END

或这个 -

ALTER PROCEDURE dbo.getstudents

      @year INT
    , @count INT

AS
BEGIN

     SELECT student_name, student_id, marks
     FROM (
         SELECT 
                 student_name
               , student_id
               , marks
               , rn = ROW_NUMBER OVER (ORDER BY marks DESC) 
         FROM (
             SELECT 
                      student_name
                    , student_id
                    , marks = SUM(marks)
             FROM dbo.tbl_Students 
             WHERE [Year] = @year
               GROUP BY 
                      student_name
                    , student_id
         ) t
     ) t2
    WHERE t2.rn <= @count

END
于 2013-07-19T10:34:28.140 回答
2

尝试这个,

ALTER  PROCEDURE getstudents

      @year INT

AS
BEGIN

    SELECT TOP 25 [student-name]
    FROM tbl_Students WHERE year=@year

END
于 2013-07-19T10:39:11.017 回答
1

您以正确的方式传递了参数。

现在您必须在 SP 中查询您想要的信息。

所以最终选择显示您提取的结果。

显然关于经销商学生、学生 i 等,你在这里写下你的数据库模型。

于 2013-07-19T10:34:24.167 回答