SQL >> Base de Datos >  >> RDS >> Sqlserver

Por qué una coma y un JOIN se comportan de forma inesperada en SQL Server 2008

JOIN tiene mayor precedencia que la coma. Por eso tu segunda sentencia se interpreta agrupando primero las combinaciones:

select sched.id, max(txn.dttm), acc.id
from PaymentSchedulePeriod sched,
     (PaymentSchedulePayment pay
      right outer join AccountTransaction txn
        on pay.accountTransactionFk = txn.id
      right outer join Account acc
        on txn.accountFk = acc.id)
where sched.accountFk = acc.id
group by sched.id, acc.id

Consulta también: JOIN precedence rules per SQL-99.

Como regla general, evita mezclar la sintaxis de coma con JOIN explícito: es una fuente habitual de resultados sorprendentes.