sql fight

Интересно как разные диалекты sql по-разному оптимизированы.

Вот было у меня две таблички A, B и это one to many, надо было левенько заджойнится с некоторыми условиями, а где не получилось с условиями — вернуть null. А если получилось несколько раз, то вернуть самое последнее справа.

Казалось бы что тут могло произвести вот это вот:

Aggregations of aggregations are not allowed at [10:8]
SELECT list expression references a.title which is neither grouped nor aggregated at [10:18]
Correlated subqueries that reference other tables are not supported unless they can be de-correlated, such as by transforming them into an efficient JOIN.
ORDER BY clause expression references column b.id which is neither grouped nor aggregated at [34:14]
HAVING clause should return type BOOL, but returns INT64 at [42:8]
Aggregate function MAX not allowed in JOIN ON clause at [24:21]
Star expansion expression references column id which is neither grouped nor aggregated at [28:19]

Решение решения сложно назвать решением, но оно решение:

select a.*, b.* from a as a
left join (select * from b where id in (
  select id from b where (cond1 = 1) and (cond2 = 2) and (cond3 = 3) group by a_id order by id desc
)) as b on (a.id = b.a_id)

2024.09.13 17:04