在 NHibernate 中选择子查询的最小值
我正在尝试将此 SQL 查询转换为 NHibernate 解决方案:
SELECT MIN(TopTimes.StartTime)
FROM (SELECT TOP 100 StartTime FROM Pack ORDER BY StartTime DESC) AS TopTimes
有效地,对于最后 X 个(在本例中为 100 个)启动的包,我想知道最小 StartTime
是多少。这看起来并不复杂,但经过 2 小时的谷歌搜索后,我无法理解它。
到目前为止,我有这样的子查询:
DetachedCriteria.For<Pack>()
.SetProjection(Projections.Property("StartTime"))
.SetMaxResults(100)
.AddOrder(Order.Desc("StartTime"));
但我不确定如何将其与 Projections.Min
Update 结合起来:详细说明一下:
示例:让 < code>select StartTime from Pack 返回以下结果:
2011-08-05 09:05:04.000
2011-08-05 08:05:04.000
2011-08-05 06:05:04.000
2011-08-05 05:05:04.000
2011-08-05 07:05:04.000
我想保留 2 个最近的开始时间:
SELECT TOP 2 StartTime FROM Pack ORDER BY StartTime DESC
返回:
2011-08-05 09:05:04.000
2011-08-05 08:05:04.000
然后我取其中的最小值 2011-08-05 08:05:04.000
这就是我所追求的。
建议的 select top 2 StartTime from Pack order by StartTime ASC
将返回:
2011-08-05 05:05:04.000
2011-08-05 06:05:04.000
这不是我需要的。
I am trying to translate this SQL query into a NHibernate solution:
SELECT MIN(TopTimes.StartTime)
FROM (SELECT TOP 100 StartTime FROM Pack ORDER BY StartTime DESC) AS TopTimes
Effectively for the last X (in this case 100) started packs I want to know what the minimum StartTime
is. This doesn't seem complicated but after 2h googling I can't wrap my head around it somehow.
I have the subquery like this so far:
DetachedCriteria.For<Pack>()
.SetProjection(Projections.Property("StartTime"))
.SetMaxResults(100)
.AddOrder(Order.Desc("StartTime"));
But I am not sure how to marry this up with the Projections.Min
Update: To elaborate a bit more:
Example: Let select StartTime from Pack
return the following result:
2011-08-05 09:05:04.000
2011-08-05 08:05:04.000
2011-08-05 06:05:04.000
2011-08-05 05:05:04.000
2011-08-05 07:05:04.000
I want to keep the 2 most recent start times:
SELECT TOP 2 StartTime FROM Pack ORDER BY StartTime DESC
which returns:
2011-08-05 09:05:04.000
2011-08-05 08:05:04.000
Then I take the minimum of that which is 2011-08-05 08:05:04.000
and that's what I am after.
The suggested select top 2 StartTime from Pack order by StartTime ASC
will return:
2011-08-05 05:05:04.000
2011-08-05 06:05:04.000
which is not what I need.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据您对问题的评论。您根本不需要使用“MIN”。
“MIN”将返回一个结果。如果您有一些分组条件,那么您可以返回每个组的最小值。但根据你的信息,这不是必需的。
目前您正在使用 DESC(降序),它将列出从最新到最旧的日期,因此您最终会说:
2011-06-15
2011-05-18
2011-05-13
2011-04-07
您想要更新查询以使用 ASC(升序),以便首先获得最早的日期,这将为您提供相同的结果,例如:
2011-04-07
2011-05-13
2011-05-18
2011-06-15
要更新您的查询,您所需要做的就是:
如果您需要将此作为子查询,那么您能否提供更多信息。
那么您想对所有结果进行排序,并从中获取最短日期吗?
然后您可以这样做:
这将为您提供最后 100 个结果中最低/最旧的单个日期结果。
Based on your comments on the question. You don't need to use 'MIN' at all.
'MIN' will return you a single result. If you had some criteria, with grouping, then you could return the minimum value of each group. But based on your information it's not required.
At the moment you're using DESC (Descending) which will list the dates from newest to oldest, so you would end up with say:
2011-06-15
2011-05-18
2011-05-13
2011-04-07
You want to update your query to use ASC (Ascending), so that you get the oldest date first, which will give you the same results like:
2011-04-07
2011-05-13
2011-05-18
2011-06-15
To update your query all you need to do is:
If you need this as a sub-query then can you please provide more information.
So you want to order all the results, and take the minimum date from that?
Then you could do:
This would give you the lowest/oldest single date result from the last 100 results results.