选择没有空值的列中的最后一个值

编程入门 行业动态 更新时间:2024-10-28 08:30:49
本文介绍了选择没有空值的列中的最后一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个 SQL SELECT 语句情况如下:

I have a SQL SELECT Statement situation as follows:

我想选择并获取带有 Aquila's Test2 标记的行,即 30 作为标记列下的最后一个值,并省略空行.换句话说,我想通过 Test# 对学生进行分组并选择不为空的最新测试标记.数据应按 Test# DESC 顺序排序.

I want to select and get the row with Aquila's Test2 mark which is 30 as the last value under the mark column and omit the null rows. In other words, I want to group the students by Test# and select the latest test mark that is not null. Data should be Sorted by Test# DESC Order.

我如何实现这一目标?请帮忙.

How do I achieve this? Please help.

我的桌子是这样的:

+------------+-------------+ ID| Test# | Student | Mark | +------------+-------------+ 1 | Test1 | Aquila | 20 | 2 | Test1 | Peter | 30 | 3 | Test1 | Jack | 40 | 4 | Test2 | Aquila | 30 | 5 | Test2 | PETER | 40 | 6 | Test2 | Jack | 50 | 7 | Test3 | Aquila | NULL | 8 | Test3 | Peter | 50 | 9 | Test3 | Jack | 60 | +------------+-------------+

我希望输出是:

+------------+-------------+ ID| Test# | Student | Mark | +------------+-------------+ 4 | Test2 | Aquila | 30 | 8 | Test3 | Peter | 50 | 9 | Test3 | Jack | 60 | +------------+-------------+

推荐答案

我终于按照如下方式自己制定了 select 语句并且它起作用了.如果有人遇到类似问题,这里是工作代码.

I finally worked out the select statement myself as follows and it worked. If someone out there has a similar problem, here is the working code.

SELECT * FROM table WHERE ID IN ( SELECT MAX(ID) FROM table WHERE mark IS NOT NULL GROUP BY Test# DESC);

更多推荐

选择没有空值的列中的最后一个值

本文发布于:2023-10-27 16:21:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1533911.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!