The APPLY operator in mssql is somewhat akin to a JOIN LATERAL in postgresql and allows referencing columns from the left side of the FROM clause on the right side of the operand.
The goal is that
from sqlalchemy import select, table, column
from sqlalchemy.dialects.mssql import apply
t1 = table('t1', column('c1'))
t2 = table('t2', column('c1'), column('c2'))
subq = select([t2.c.c2]).where(t2.c.c1 == t1.c.c1).subquery()
stmt = select([t1.c.c1, subq.c.c2]).select_from(apply(t1, subq))
would produce something close to
SELECT t1.c1, anon_1.c2 FROM
t1 CROSS APPLY (SELECT t2.c2 FROM
t2 WHERE t2.c1 = t1.c1) AS anon_1
It is maybe noteworthy that Oracle has also implemented { CROSS | OUTER } APPLY and so maybe this feature should live in the generic SQL namespaces, instead of the mssql dialect.
The APPLY operator in mssql is somewhat akin to a JOIN LATERAL in postgresql and allows referencing columns from the left side of the FROM clause on the right side of the operand.
The goal is that
would produce something close to
It is maybe noteworthy that Oracle has also implemented
{ CROSS | OUTER } APPLYand so maybe this feature should live in the generic SQL namespaces, instead of the mssql dialect.