[英]NHibernate QueryOver<> - Aggregate function over SubQuery
How can I write the following SQL statement using QueryOver<> syntax? 如何使用QueryOver <>语法编写以下SQL语句?
SELECT COUNT(*) FROM (
SELECT FirstName,LastName
FROM People
GROUP BY FirstName, LastName
) as sub_t
I have the inner query working so far: 到目前为止,我有内部查询工作:
var q = _session.QueryOver<Person>()
.SelectList(l => l
.SelectGroup(x => x.FirstName)
.SelectGroup(x => x.LastName));
But I have no idea how to wrap this in a subquery and get a row count out of it. 但是我不知道如何将它包装在子查询中并从中获取行数。 Can it be done?
可以吗?
Unfortunately my RDBMS dialect (MsSqlCe40Dialect) does not support COUNT DISTINCT so I do not have the benefit of using SelectCountDistinct(). 不幸的是我的RDBMS方言(MsSqlCe40Dialect)不支持COUNT DISTINCT,所以我没有使用SelectCountDistinct()的好处。
I am not familiar with QueryOver, but I have used the following aggregate function when a sub query was not possible for this type of count, thought it might be useful, and while posting discovered a few issues I wasn't aware of previously so I posted them too. 我不熟悉QueryOver,但是当一个子查询不可能用于这种类型的计数时,我使用了以下聚合函数,认为它可能有用,并且在发布时发现了一些我以前不知道的问题所以我发布了它们。
Note: it is about 10x slower with moderate data amounts. 注意:对于中等数据量,它大约慢10倍。
Aggregate method 聚合方法
SELECT
COUNT(DISTINCT FirstName+LastName )
FROM People
Accommodate for special cases 适用于特殊情况
similar combination names "Joe Smith" vs "Joes Mith" (Assumes ~ is not in your dataset) 类似的组合名称“Joe Smith”vs“Joes Mith”(假设〜不在您的数据集中)
SELECT
COUNT(DISTINCT FirstName+'~'+LastName )
FROM People
nulls (Assumes ^ is not in your dataset) nulls(假设^不在您的数据集中)
SELECT
COUNT(DISTINCT IsNull(FirstName,'^')+'~'+IsNull(LastName,'^') )
FROM People
Trailing white space, seems RTRIM is intrinsic to Group By 尾随空白,似乎RTRIM是Group By固有的
SELECT
COUNT(DISTINCT IsNull(RTrim(FirstName),'^')+'~'+IsNull(Rtrim(LastName),'^') )
FROM People
Benchmarking (80k rows of data on AMD single Quad Core) 基准测试 (AMD单核四核上80k行数据)
80-100ms - run Sub Query Method (see OP) 80-100ms - 运行子查询方法(参见OP)
800-1200ms - aggregate method with distinct, accommodating for special cases doesn't seem to make much noticeable difference. 800-1200ms - 具有明显的聚合方法,适用于特殊情况似乎没有太大的显着差异。
Is it not possible for you to use the RowCount property of the IQueryOver? 您是否不可能使用IQueryOver的RowCount属性? Like this:
像这样:
var totalRows = _session.QueryOver<Person>()
.SelectList(l => l
.SelectGroup(x => x.FirstName)
.SelectGroup(x => x.LastName)).RowCount();
Ok, I don't know the reasons behing using QueryOver, but I would do something like this, I think it will give you what you are looking for: 好吧,我不知道使用QueryOver的原因,但我会做这样的事情,我想它会给你你想要的东西:
Session.CreateCriteria<Person>()
.SetProjection(
Projections.ProjectionList()
.Add(Projections.GroupProperty("FirstName")))
.Add(Projections.GroupProperty("LastName")))
.List<Person>().Count();
Hope that helps... 希望有帮助......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.