[SQL] Lateral | JeongKeepsCalm

[SQL] Lateral

Lateral

1
2
3
4
5
6
7
8
select * from bys_test_tobe.testEvent t1
left join 
(
	select * from bys_test_tobe.testEvent t2
	where t2.date_at > t1.date_at
	order by t2.date_at asc
	limit 1
) t2 on t1.id = t2.id

ERROR 발생:
SQL Error [42P01]: ERROR: invalid reference to FROM-clause entry for table “t1”
Hint: There is an entry for table “t1”, but it cannot be referenced from this part of the query.


1
2
3
4
5
6
7
8
select * from test t1
left join lateral 
(
	select * from test t2
	where t2.date_at > t1.date_at
	order by t2.date_at asc
	limit 3
) t2 on true;

lateral 은 외부 스코프에 접근이 가능하여 사용 가능하다.