我有两个表;#tempA 和 #tempB 我想连接Student_id和Admit_date匹配的两个表。 我想在student_id和录取日期匹配的行之后立即返回 #tempB 表行。 在这种情况下,我想返回第 2 行、第 5 行和第 18 行 #tempB。 我无法让它工作。
温度A
Student_id Admit_date
709652 2020-01-31
709652 2019-10-09
368671 2015-04-19
温度B
Unique_id Student_id Admit date
1 709652 2020-01-31
2 709652 2019-12-16
3 709652 2019-12-04
4 709652 2019-10-09
5 709652 2019-10-07
6 709652 2019-09-22
7 709652 2019-09-21
8 709652 2019-08-29
9 709652 2019-08-01
10 709652 2019-07-01
11 709652 2019-06-24
12 709652 2019-03-25
13 709652 2019-03-14
14 709652 2014-03-31
15 709652 2016-06-23
16 709652 2014-05-14
17 368671 2015-04-19
18 368671 2014-04-10
19 368671 2014-04-01
20 368671 2014-03-21
您可以使用子查询:
SELECT a.Student_id , a.Admit_date,
( SELECT MIN( b.Admit_date)
FROM tempB b
WHERE b.Admit_date >= a.Admit_date
AND a.Student_id = b.Student_id
) as Admit_date2
FROM tempA a