sqlite-如何按日期选择不同的行?

jyztefdp  于 2021-07-26  发布在  Java
关注(0)|答案(2)|浏览(290)

我有一个模式如下:

CREATE TABLE fruit ( name TEXT, date_collected TEXT);

数据:

INSERT INTO fruit (name, date_collected) VALUES ('red apples', '2020-02-04 08:04:37+00:00');
INSERT INTO fruit (name, date_collected) VALUES ('red apples', '2020-06-01 04:36:09+00:00');
INSERT INTO fruit (name, date_collected) VALUES ('mangoes', '2020-03-18 19:14:24+00:00');
INSERT INTO fruit (name, date_collected) VALUES ('mangoes', '2017-04-20 19:14:24+00:00');
INSERT INTO fruit (name, date_collected) VALUES ('watermelon', '2019-12-20 14:29:02+00:00');

如何选择只按日期显示最新不同水果条目的输出?
例如

red apples | 2020-06-01 04:36:09+00:00
mangoes | 2020-03-18 19:14:24+00:00
watermelon | 2019-12-20 14:29:02+00:00
ftf50wuq

ftf50wuq1#

如果只需要最长日期,请使用聚合:

select name, max(date_collected)
from fruit
group by name;
ugmeyewa

ugmeyewa2#

这应该适合您:

select
    Name
    ,max(convert(date,date_collected)) [MostRecentDate]

from
    fruit

group by
    Name

order by
    [MostRecentDate] desc

相关问题