我有两个类 ComRecordMain 和 ComRecordFeeder 。
//Tabel name is TBL_RecordMains
public class ComRecordMain {
public virtual long Id { get; set; } //Key, Auto increment
public virtual ComDevice Device { get; set; } //Column name is 'deviceId'
public virtual int Date { get; set; }
public virtual int Hour { get; set; }
public virtual float Stotal { get; set; }
public virtual float In { get; set; } //Column name is '_in'
public virtual int Unbalance { get; set; }
}
//Tabel name is TBL_RecordFeeders
public class ComRecordFeeder {
public virtual ComRecordMain MainRecord { get; set; } //Composite Key
public virtual int FeederIndex { get; set; } //Composite Key
public virtual float Stotal { get; set; }
public virtual float In { get; set; } //Column name is '_in'
public virtual int Unbalance { get; set; }
}
我需要分析:
如果Feeder中每天最多总,请为每个Feeder选择不平衡值
我尝试为此T-Sql创建nHibernate查询:
SELECT
date,
[FeederIndex],
max([Unbalance]) as [Unbalance]
FROM
(
SELECT
T1.date,
T1.[Unbalance],
T1.[FeederIndex]
FROM
(
SELECT
R1.date,
R2.Stotal,
R2.[FeederIndex],
R2.[Unbalance]
FROM
[TBL_RecordMains] AS R1
JOIN
[TBL_RecordFeeders] AS R2
ON
R1.ID = R2.Id
WHERE
deviceId = 30 AND[date] >= 35064 AND[date] <= 35428
) AS T1
JOIN
(
SELECT
date,
FeederIndex,
max(W2.stotal) AS smax
FROM
[TBL_RecordMains] AS W1
JOIN
[TBL_RecordFeeders] AS W2
ON
W1.Id = W2.Id
WHERE
deviceId = 30 AND[date] >= 35064 AND[date] <= 35428
GROUP by
[Date],
[FeederIndex]
) AS T2
ON
T1.date = T2.date AND
T1.FeederIndex = T2.FeederIndex AND
T1.Stotal = T2.smax
) AS T3
Group by
date,
[FeederIndex]
现在,我编写了最小的NHibernate查询来获取所有数据,并使用linq进行聚合并计算结果。
用于创建我的结果。
如何将此SQL转换为NHibernate查询?