我想加入两个表概述和变压器位置表。我想找到每一个设备的最新记录,然后加入位置表。
[HttpGet]
[Route("overview_locations")]
public HttpResponseMessage GetoverviewLocations()
{
var overviewDetails = _dbContext.Overviews.ToList();
if (overviewDetails.Count != 0)
{
return Request.CreateResponse(HttpStatusCode.OK, overviewDetails, Configuration.Formatters.JsonFormatter);
}
else
{
}
}样本数据
2020-11-23 01:03:07.000 8673220311024 0 0 23.842163 91.280693
2020-11-23 01:01:06.000 8673220311024 0 0 23.842163 91.280693
2020-11-23 01:00:00.000 8645020301067 0 0 23.841940 91.280306Exp op
2020-11-23 01:03:07.000 8673220311024 0 0 23.842163 91.280693
2020-11-23 01:00:00.000 8645020301067 0 0 23.841940 91.280306sql
select *
from
(select
o.devicetimestamp,
o.deviceimei,
o.oti_a,
oti_t,
ts.latitude,
ts.longitude,
row_number() over(partition by o.deviceimei order by o.devicetimestamp desc) rn
from overview
inner join transformerlocations ts
on o.deviceimei = ts.imei
where ts.latitude is not null) t
where rn = 1发布于 2020-11-25 20:26:04
要按第一个ROW_NUMBER()转换筛选,您需要按PARTITION分组,然后按需要对每个组进行排序,并在每个组中选择第一个项。
var ans = from o in overview
join ts in transformerlocations on o.deviceimei equals ts.imei
where ts.latitude != null
group new { o, ts } by o.deviceimei into otsg
select (
from ots in otsg
orderby ots.o.devicetimestamp descending
select new {
ots.o.devicetimestamp,
ots.o.deviceimei,
ots.o.oti_a,
ots.ts.latitude,
ots.ts.longitude
}
).FirstOrDefault();https://stackoverflow.com/questions/64976690
复制相似问题