i want to join 2 tables overview and transformerlocations table. i want to find the latest record of each deviceimei and then join with locations table.
[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
{
}
}
sample data
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.280306
Exp 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.280306
sql
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