SQLite - 选择一列的DISTINCT并获取其他列

时间:2015-03-05 18:17:51

标签: sqlite

之前在本网站上已经触及过这个问题。我想要小组,但我也希望得到其他领域。我需要的是每组最低的id,但我得到的是最高的。我已经尝试过variod SQL查询,最近的2个工作是

1)

select * 
from reminder 
group by Eventgroup 
order by autoid

2)

SELECT distinct Autoid,EventDate,Subject,birthdate,Eventgroup 
from reminder 
group by Eventgroup 
order by autoid

数据:

EventDate    Subject          birthdate       Eventgroup    autoid

09/10/2017   Joes Birthday     09/10/1995      4            9
13/07/2017   Bill Birthday     13/07/1999      2            8
04/04/2017   Tony Birthday     04/04/1993      3            7
09/10/2016   Joes Birthday     09/10/1995      4            6
13/07/2016   Bill Birthday     13/07/1999      2            5
04/04/2016   Tony Birthday     04/04/1993      3            4
09/10/2015   Joes Birthday     09/10/1995      4            3
13/07/2015   Bill Birthday     13/07/1999      2            2
04/04/2015   Tony Birthday     04/04/1993      3            1

这两个查询都返回

09/10/2017   Joes Birthday     09/10/1995      4            9
13/07/2017   Bill Birthday     13/07/1999      2            8
04/04/2017   Tony Birthday     04/04/1993      3            7

我想要的是耳机日期,例如

09/10/2015   Joes Birthday     09/10/1995      4            3
13/07/2015   Bill Birthday     13/07/1999      2            2
04/04/2015   Tony Birthday     04/04/1993      3            1

1 个答案:

答案 0 :(得分:0)

使用子查询加入表,查找每个事件组的最早日期。

SELECT a.*
FROM reminders a
JOIN (SELECT eventgroup, MIN(eventdate) mindate
      FROM reminders
      GROUP BY eventgroup) b
ON a.eventgroup = b.eventgroup AND a.eventdate = b.mindate

这与重复问题中this answer中的第二个查询结构相同。

DEMO