我有一个查询,查找指定技术人员未使用所有分配小时数的作业。
在SQL中,我可以使用having子句来查询总和。
SELECT J.Jobid, J.HoursAssigned, SUM(JT.HoursUsed)
FROM Job J
JOIN JobTechnician JT ON JT.JobId = J.JobId
GROUP BY J.Jobid, J.HoursAssigned
HAVING J.HoursAssigned <> SUM(JT.HoursUsed)
这会生成以下数据集
JOBID | HOURSASSIGNED | HOURSUSED
-----------------------------------
660015 | 10 | 8
660016 | 20 | 18
660017 | 30 | 28
660018 | 40 | 38
我可以在Nhibernate中重新创建查询作为投影但是如何将当前存在的限制置于SQL having子句中?
ICriteria criteria = base.Session.CreateCriteria(typeof(Job));
criteria.CreateAlias("Job.JobTechnicianList", "JobTechnician", JoinType.InnerJoin);
criteria.SetProjection(
Projections.ProjectionList()
.Add(Projections.Property("Job.JobId", "JobId")
.Add(Projections.Property("Job.HoursAssigned", "HoursAssigned")
.Add(Projections.Sum("JobTechnician.HoursUsed"), "HoursUsed")
.Add(Projections.GroupProperty("Job.JobId"))
.Add(Projections.GroupProperty("Job.HoursAssigned"))
);