如何从sql server中的列中选择多个最大值?

时间:2018-02-01 10:13:03

标签: sql sql-server

我有两个表TBLSession和TBLStudentFeeRecord有以下样本数据集

TBLSession:

   SessionId    SessionName  SessionStartMonth       SessionEndMonth
      1           2018-2019    2018-03-24               2019-02-24
      2           2019-2020    2019-01-30               2019-12-30
      3           2020-2021    2020-01-30               2021-12-30

TBLStudentFeeRecord:
  StudentId     SessionId   TutionFee   BranchId    ClassId   SectionId
    1001                1     1000       1              1         1
    1001                2     2000       1              3         1
    1001                3     1000       2              2         1

现在,我想要实现的是选择两个最大会话TutionFee选择StudentId。我可以使用max(columnName)从该特定列中获取一个最大值。现在我怎么能得到两个最大会话? 查询这些表后需要以下数据集

ResultDataSet:
  StudentId     SessionId   TutionFee   SessionName      

    1001                2     2000       2019-2020            
    1001                3     1000       2020-2021            

实现上述数据集的查询是什么?

7 个答案:

答案 0 :(得分:1)

由于您尚未发布尝试,因此请尝试以下方法:

伪查询:

@ComponentScan("you.configurations.base.package")

答案 1 :(得分:1)

您可以在最大TutionFee和SessionId

上使用子选择前2名
select a.* , t.TutionFee
from TBLSession a
inner JOIN (
select TOP 2 studentID, TutionFee, SessionId 
from TBLStudentFeeRecord 
where StudentId = 1001
order TutionFee desc, SessionID desc
) t on t.SessionId = a.SessionId  

答案 2 :(得分:1)

试试这个:

SELECT StudentId ,SessionId,TutionFee,SessionName 
FROM(
    SELECT TSF.StudentId ,TSF.SessionId,TSF.TutionFee,TS.SessionName
        ,ROW_NUMBER() OVER(PARTITION BY TSF.TutionFee ORDER BY SessionId DESC)RN
    FROM TBLStudentFeeRecord TSF
    INNER JOIN TBLSession TS ON TS.SessionId=TSF.SessionId
    )D
WHERE RN=1

答案 3 :(得分:1)

这应该有效

select TBLStudentFeeRecord.StudentId,  TBLStudentFeeRecord.SessionId, TBLStudentFeeRecord.TutionFee, TBLStudentFeeRecord.SessionName
from TBLStudentFeeRecord
inner join TBLSession on TBLSession.SessionId = TBLStudentFeeRecord.SessionId
where TBLStudentFeeRecord.StudentId =  1000
ORDER BY TBLStudentFeeRecord.TutionFee DESC
LIMIT 2

答案 4 :(得分:1)

如果您想获得每位学生的2个会话信息,可以将SQL Row_Number function与Partition By子句一起使用

否则,使用Row_number()函数而不使用Partition By子句

您可以在以下SQL CTE语句中找到两种替代方法的解释

;with cte as (
select *, 
-- top 2 session per student
--  rn = ROW_NUMBER() over (partition by studentid order by sessionid desc)
-- top 2 sessions
    rn = ROW_NUMBER() over (order by sessionid desc)
from TBLStudentFeeRecord
)
select
*
from cte
inner join TBLSession on TBLSession.sessionid = cte.sessionid
where rn <= 2

输出如下

enter image description here

您可以根据需要修改选择列表

答案 5 :(得分:1)

一般来说,如果您遇到任何困难或任何复杂的场景,只需使用存储过程

,查询语言就会很强大

两个最大..........................................

CREATE PROCEDURE twoMaxFrom()
  BEGIN
    DECLARE max1 DOUBLE;
    DECLARE max2 DOUBLE;

    DECLARE emp_cursor CURSOR FOR SELECT
                                    TutionFee
                                  FROM TBLStudentFeeRecord;
    -- 2. Declare NOT FOUND handler
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET finished = 1;
    -- 3. Open the cursor
    OPEN emp_cursor;
    L: LOOP
      -- 4. Fetch next element
      FETCH emp_cursor
      INTO TutionFee;
      -- Handler will set finished = 1 if cursor is empty
      IF finished = 1
      THEN
        LEAVE L;
      END IF;
      SET max1 = 0;
      SET max2 = 0;
      IF max1 > max2 
      THEN
        SET max1 = TutionFee;
        SET max2 = max1;

      END IF;
    END LOOP;
    -- 5. Close cursor when done
    CLOSE emp_cursor;
    SELECT max1;
    SELECT max2;
  END;

答案 6 :(得分:0)

您可以使用joinTOP

来获取它
SELECT TOP 2 StudentId
    ,t1.SessionId
    ,TutionFee
    ,SessionName
FROM TBLSession AS t1
INNER JOIN TBLStudentFeeRecord AS t2 ON t1.SessionId = t2.SessionId
WHERE t2.StudentId = 1001
ORDER BY t1.SessionId DESC