gpt4 book ai didi

sql - 选择一列中具有非空值的所有最新记录不同关键字

转载 作者:行者123 更新时间:2023-11-29 02:38:04 25 4
gpt4 key购买 nike

从这个问题开始 SELECT the newest record with a non null value in one column

我知道我有这些数据的地方有问题

id | keyword | count | date
1 | ipod | 200 | 2009-08-02
2 | ipod | 250 | 2009-09-01
3 | ipod | 150 | 2009-09-04
4 | ipod | NULL | 2009-09-07
5 | apple | 100 | 2009-07-01
6 | apple | 98 | 2009-07-05
7 | apple | 500 | 2009-07-30
8 | itunes | NULL | 2009-08-30
9 | itunes | 50 | 2009-09-10
10 | itunes | NULL | 2009-09-15

并且需要一个查询来取出行3、7 和 9具有最新日期且非空的行。

最佳答案

使用:

SELECT t.id,
t.keyword,
t.count,
t.date
FROM TABLE t
JOIN (SELECT t.keyword,
MAX(t.date) 'max_date'
FROM TABLE t
WHERE t.count IS NOT NULL
GROUP BY t.keyword) x ON x.keyword = t.keyword
AND x.max_date = t.date

关于sql - 选择一列中具有非空值的所有最新记录不同关键字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1740280/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com