一个监护人有3个约会。
我想返回第一个日期,然后返回它的前一个日期。
数据应类似于:
custid first previous
11 2019-06-10 2019-06-15
11 2019-06-10 2019-07-10
我已经做完了,但是无法将其放在第二行。
SELECT A.PersonID ,min(a.date) as first,b.date as previous,c.date as previous from Persons as a
INNER JOIN (select PersonID ,date from Persons) b
on a.PersonID
=b.PersonID AND
a.date<
b.date
INNER JOIN (select PersonID ,date from Persons) c
on b.PersonID=
c.PersonID
AND b.date<
c.date
我得到的输出:
personid first previous previous
11 2019-06-10 2019-06-15 2019-07-10
我希望它看起来像:
custid first previous
11 2019-06-10 2019-06-15
11 2019-06-10 2019-07-10
答案 0 :(得分:0)
您不需要添加第三个内部联接表。由于您将min()与其他列一起使用,因此应伴随Group By。
select
a.PersonID as custid,
min(a.date) as first,
b.date as previous
from
Persons as a
inner join Persons b on a.PersonID = b.PersonID and b.date > a.date
group by a.PersonID, b.date