I am basically trying to create this query using the NHibernate ICriteria interface:
SomeTable 1: n AnotherTable
SomeTable has columns: PrimaryKey, NonAggregateColumn
AnotherTable has columns: PrimaryKey, ForeignKey, AnotherNonAggregate, YetAnotherNonAggregate
SELECT table1.NonAggregateColumn, subquery.SubQueryAggregate1, subquery.SubQueryAggregate2 FROM SomeTable AS table1 LEFT JOIN ( SELECT table2.ForeignKey, COUNT(table2.AnotherNonAggregate) AS SubQueryAggregate1, AVG(table2.YetAnotherNonAggregate) AS SubQueryAggregate2 FROM AnotherTable AS table2 GROUP BY (table2.ForeignKey) ) AS subquery ON subquery.ForeignKey = table1.PrimaryKey
It is clear that using the Projection subquery is not very effective, because SQL has to double-scan the table (one projection subquery per population).
Using multiple GROUP BYs is also inefficient.
Is there a solution for this? So far I have resorted to using raw SQL, but this is becoming cumbersome for complex reports.
alias left-join subquery nhibernate criteria
Raif atef
source share