简体   繁体   English

nhibernate通过组永久表联合的ROLLUP()与组进行复杂的sql查询所有派生查询

[英]nhibernate complex query of sql with a group by ROLLUP() of permenant table union all derived query

How would you convert the following sql that contains a case count, group by, union all to nhibernate? 您如何将下面的包含案例数的SQL分组(按分组,并集所有)转换为nhibernate? This complex sqql with a group by ROLLUP() of permanent table union all derived query columns 带有永久表联合的ROLLUP()组的复杂sqql所有派生查询列

 select CASE WHEN GROUPING([BUSINESS_UNIT]) = 1 THEN 'Total' ELSE [BUSINESS_UNIT] END [BUSINESS_UNIT] , SUM(InjuryIllnessTtlCount) InjuryIllnessTtlCount from ( select i.INCIDENT_ID ,b.BUSINESS_UNIT ,case when (i.INJURY_ILLNESS_TYPE_ID=4 ) then 1 --'Injury/Illness (Near Miss with High Severity Potential)' else 0 end as InjuryIllnessTtlCount from tblIncident i join tblBUSINESS_UNIT b on i.BUS_UNIT_ID = b.BUS_UNIT_ID --where (CONVERT(varchar(10),i.create_dt, 111) between '2015/01/12' and '2015/05/12') union all select 0 incident_id ,b.BUSINESS_UNIT ,0 InjuryIllnessTtlCount from tblBUSINESS_UNIT b ) abc group by ROLLUP(abc.BUSINESS_UNIT) 

When it comes to this complicated queries, the conversion to NHibernate either not possible or does not worth it. 当涉及到这种复杂的查询时,转换为NHibernate要么不可能,要么不值得。 So my two cents is to go with, 所以我要花两分钱

var query = <your query>;

var session = sessionFactory.OpenSession();
var result =session.CreateSQLQuery(query)
                .List();

Where the Query is directly executed. 直接执行查询的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM