nhibernate复杂查询sql与一组由ROLLUP()的永久表联合所有派生查询

时间:2015-05-13 20:24:54

标签: sql sql-server nhibernate

如何将包含案例计数,group by,union all的以下sql转换为nhibernate? 这个复杂的sqql与一个由ROLLUP()组成的永久表联合所有派生查询列



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)




1 个答案:

答案 0 :(得分:0)

当涉及到这些复杂的查询时,转换为NHibernate既不可能也不值得。所以我的两分钱就是这样,

var query = <your query>;

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

直接执行查询。