我的要求是获取SiteItemOnHand记录,按Businessunitid分组,inventoryitemid,并在每个具有max(lastmodifiedtimestamp)的组中获取整个SiteItemOnHand记录。
我正在尝试为下面的sql查询编写nhibernate等效查询
Select x.* from
(
Select businessunitid,inventoryitemid,max(lastmodifiedtimestamp) as maxvalue from InventoryItemBUOnHand
group by businessunitid,inventoryitemid HAVING inventoryitemid in (939) and businessunitid=829
) as x
inner join
(
Select businessunitid,inventoryitemid,lastmodifiedtimestamp,inventoryitemonhandid from InventoryItemBUOnHand
where inventoryitemid in (939) and businessunitid=829
) as y
on x.businessunitid=y.businessunitid and x.inventoryitemid =y.inventoryitemid and x.maxvalue=y.lastmodifiedtimestamp
我有很多限制。我只允许使用Nhibernate 3.0版本,并且严格禁止编写sql查询(标准)。我只能写下面查询的一半。任何帮助将不胜感激。
var query= _session.QueryOver<SiteItemOnHand>()
.Where(x => x.Businessunitid == siteID)
.Where(x => x.End == endDate)
.Where(x => x.CountID != filterCount.ID)
.WhereRestrictionOn(x => x.ItemID).IsIn(itemIdList.ToArray())
.SelectList(list => list
.SelectMax(x => x.LastModifiedTimestamp)
.SelectGroup(x => x.Businessunitid)
.SelectGroup(x => x.ItemId));
上面的查询正在生成下面的SQL查询,它只返回三列,但我需要整个记录,因为在检索记录后我需要更新。我需要获取满足上述查询的所有SiteItemOnHand记录。
SELECT max(this_.lastmodifiedtimestamp) as y0_, this_.businessunitid as y1_, this_.inventoryitemid as y2_
FROM InventoryItemBUOnHand this_
WHERE this_.businessunitid = 567 and this_.enddate = '1/31/2019 1:18:17 AM' and not (this_.itemcountid = 958)
and this_.inventoryitemid in (744,755)
GROUP BY this_.businessunitid, this_.inventoryitemid
任何帮助将不胜感激。如果您需要更多信息,请告诉我。
神不在的星期二
相关分类