简体   繁体   English

如何从需要在单个查询中首先添加到sql server中的表中获得第二高的表?

[英]How to get the 2nd highest from a table where it need to be added first in sql server in a single query?

I was asked this question in an interview, this the table 面试中有人问我这个问题,这张桌子

Roll | Sub | Marks
 1      A     20
 1      B     21
 2      A     15
 2      B     19
 3      A     21
 3      B     22

now i have to find the roll and marks 2nd highest marks obtained by the student 现在我必须找到该卷并获得该学生获得的第二高分

so i answered this : 所以我回答了这个:

 declare @trytable table
 (
   roll int,
   total int
 )
 insert @trytable
 select Roll, SUM(Marks)
 from Student
 group by Roll


 Select *
 from @trytable t
 where t.total in (select MAX(total) from @trytable where total not in ( select 
 MAX(total) from @trytable)) 

which is giving the correct answer but the interviewer wanted this to be done in single query by not using the table variable 这给出了正确的答案,但访问员希望通过不使用表变量在单个查询中完成此操作

the result should be 结果应该是

 Roll | Total Marks
  1        41

so how can i do that ... please let me know 所以我该怎么做...请让我知道

Below query gives the roll numbers who obtained 2nd highest marks summing the two subject marks. 下面的查询给出了将两个主题标记相加得出的第二高分的卷数。

SELECT TOP 1 Roll, Marks
FROM 
(
    SELECT DISTINCT TOP 2 Roll, 
        SUM(Marks) over (Partition by Roll) Marks
    FROM 
        YourTable
    ORDER BY marks DESC
) temp
ORDER BY Marks 

OR 要么

SELECT 
    DISTINCT Roll,
    Marks, 
    SRANK 
FROM
(
    SELECT 
        Roll,
        Marks,
        DENSE_RANK() OVER( ORDER BY Marks DESC) AS SRANK 
    FROM
    (
        SELECT 
            Roll,
            SUM(Marks) over (Partition by Roll) Marks
        FROM YourTable
    )x
)x
WHERE SRANK=2

If I understand you correctly, you just want to get the total score for the second highest student, and student is identified by roll? 如果我对您的理解正确,那么您只想获取第二高的学生的总成绩,而该学生是否按成绩进行识别? If so: 如果是这样的话:

select roll, sum(Marks) from Student group by roll order by total limit 1,1;

Not 100% sure about the 1,1 - what you are saying is, I only want 1 row, and not the first. 不是100%知道1,1-您所说的是,我只想要1行,而不是第一行。

It can also be done through simple query: 也可以通过简单的查询来完成:

select Marks from trytable where N = (select count(distinct Marks) from trytable b where a.Marks <= b.Marks)
where N = any value

Or 要么

SELECT Roll,Marks 
FROM tableName WHERE Marks =
       (SELECT MIN(Marks ) FROM 
             (SELECT TOP (2) Marks 
              FROM tableName 
              ORDER BY Marks DESC) )

You can use analytic functions like RowNumber() 您可以使用RowNumber()之类的分析函数

select * from
(Select t.*, RowNumber() over (order by Total desc) as rownum from trytable )
where rownum = 2

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从表中获得最高,然后是最低,然后是第二高,然后是第二低值等等 - How to get the Highest, then the lowest, then the 2nd higest, and then the 2nd lowest value and so on from a table 如何创建一个 sql 查询获取表中的第 2 个到最大结果? - How to create a sql query get the 2nd to Max result in table? 如何在多对多关系中编写查询,以从SQL查询中的第二个表中获取随机1条记录 - How to write query in 1 to many relation, to get random 1 record from 2nd table in sql query SQL-表中第二高的记录 - SQL - 2nd highest record in table SQL Query从表中获取前两个最高列值 - SQL Query to get first two highest column values from a table SQL查询根据第一个表的输入从第二个表中获取字段 - SQL query to get fields from 2nd table based on input from 1st 在 where 子句中使用子查询从表中选择第二个最高日期 - Using a subquery in the where clause to select 2nd highest date from a table 如何从第一个表中获取第二个表值重复的行数 - How to get the 2nd table value repeated for number of rows from the first table 如何将某些列数据从第一表移动到第二表。 并在单个查询中用不同的数据填充第二张表的其他列 - How to move certain column data from 1st table to 2nd table. And filling other columns of 2nd table with different data in a single query SQL查询与第一和第二最高薪水有关 - Sql query related to 1st and 2nd highest salary
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM