简体   繁体   English

取决于两列的SQL TOP 10查询

[英]SQL Query for TOP 10 depending on two columns

I am new to SQL (beginner programmer), so I apologize if this might seem like a simple question. 我是SQL的新手(初级程序员),因此,如果这看起来像是一个简单的问题,我深表歉意。 I am trying to create a table on my website that displays the lowest 10 grades along with some information about the student. 我试图在我的网站上创建一个表格,该表格显示最低的10年级以及有关该学生的一些信息。 I am pretty comfortable from this aspect, but I am having trouble coding the write SQL query. 从这方面来说,我感到很舒服,但是在编写写SQL查询时遇到了麻烦。 I am using SQL Server 2008. 我正在使用SQL Server 2008。

I have a table in my database with 10 columns and 500 rows. 我的数据库中有一个表,其中有10列500行。 Two of these columns contain grades (grade1 and grade2). 这些列中的两个包含等级(等级1和等级2)。 My goal is to display in my website table the TOP 10 lowest GRADE1, but if GRADE1 is NULL I want it to take GRADE2 into consideration and display that instead. 我的目标是在我的网站表中显示前10位最低的GRADE1,但是如果GRADE1为NULL,我希望它考虑GRADE2并显示它。 So in context, if a student named Billy has no GRADE1 (its NULL) but his GRADE2 is the lowest of all (GRADE1's AND GRADE2's combined), he should be first in the list. 因此,在上下文中,如果一个名叫Billy的学生没有GRADE1(其NULL),但他的GRADE2是最低的(GRADE1和GRADE2的总和),则他应该在列表中排第一。

I would really appreciate help making a query capable of accomplishing this task, I have been researching for a solution but it has only confused me more. 我一直在为使查询能够完成此任务提供帮助,我一直在寻找解决方案,但它只会使我更加困惑。

In SQL Server you want to use isNull() 在SQL Server中,您要使用isNull()

SELECT TOP 10 isNull(grade1,grade2) AS `Grade`
FROM mytable
ORDER BY Grade DESC

You can use case in the order by clause 您可以在order by子句中使用案例

select top 10 * 
from students 
order by case when grade1 is null then grade2 else grade1 end desc

EDIT 编辑

Following BillyCode comment on including only those students that apears 3 or more times in the table I suggest this 我在BillyCode上发表评论说,只把那些表三遍或三遍以上的学生包括在内时,我建议这样做

select top 10 s.*  
from students s
inner join (select StudentId, Count(*) as total from students) c on s.StudentId = c.StudentId  
where c.total >= 3
order by case when grade1 is null then grade2 else grade1 end desc

But I'm not sure if you can join to a subquery. 但是我不确定您是否可以加入子查询。

The COALESCE function does what you want. COALESCE函数COALESCE您的需求。 COALESCE(GRADE1, GRADE2) will display GRADE1 if not null, or GRADE2 if GRADE1 is null. COALESCE(GRADE1, GRADE2)将显示GRADE1如果不为空,或GRADE2如果GRADE1为空。

So instead of ORDER BY GRADE1 , you can do ORDER BY COALESCE(GRADE1, GRADE2) 因此,您可以执行ORDER BY COALESCE(GRADE1, GRADE2)来代替ORDER BY GRADE1

See here for more details about COALESCE . 有关COALESCE更多详细信息, 请参见此处

Try this: 尝试这个:

select Top 10 
student , (case when grade1 is null then grade2 else grade1 end ) as g1 , grade2 as g2 from table order by g1 desc

Another method is to first find the 10 students with the lowest grade1. 另一种方法是首先找到10年级最低的学生。 Rename grade1 into grade. 将1级重命名为Grade。 And then find the 10 students with the lowest grade2. 然后找到10个最低2年级的学生。 Rename grade2 into grade. 将2级重命名为2级。 After that union these two results. 在合并之后,这两个结果。 From there, find the 10 students with with lowest grade. 从那里找到10个成绩最低的学生。 The SQL is something like: SQL类似于:

SELECT id, grade
FROM   (SELECT   id, grade1 AS grade
        FROM     students
        ORDER BY grade1 DESC
        LIMIT 10
        UNION
        SELECT   id, grade2 AS grade
        FROM     students
        ORDER BY grade2 DESC
        LIMIT 10)
ORDER BY grade DESC
LIMIT 10

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM