I am trying to find a problem with the query that I have. The query is actually generated using hibernate from HQL, but the resulting SQL does not do what I expect. Changing SQL leads to the correct result a little, but I'm not sure why the change should have any meaning.
Original query (does not return rows)
select sched.id, max(txn.dttm), acc.id from PaymentSchedulePeriod sched cross join 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
Modified query - cross-connect is replaced with a comma (implicit cross-connect)
Returns one row
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
My understanding, which may be incorrect, is that the entry from Table1 a, Table2 b matches the entry from Table 1 a cross join Table2 b . Therefore, I do not understand why queries return different results.
Is this something related to the interaction between the cross join and the outer joins in the first request that causes this? I looked at the query plans, and the second query plan seems reasonable. The first of them has no external connections in everything that is strange.
This is on SQLServer 2008.
Mike q
source share