I have a question Simmilar to this one except I have a table that looks like this:
Temp_Date Building_ID Sector_ID Temperature
[Date/Time] [I32] [I32] [DBL]
1/9/2018 4:14:31 AM 456 0 20.23
1/9/2018 4:15:14 AM 123 1 35.23
1/9/2018 4:16:21 AM 123 0 15.23
1/9/2018 4:15:45 AM 123 2 25.23
1/9/2018 4:16:21 AM 456 0 25.23
1/9/2018 4:16:59 AM 123 1 35.23
I would like to Get a result data for the latest logged temperature for each unique building/sector combination.
For the example dataset, the table I'm looking for would look like
Building_ID Sector_ID Temperature
123 0 15.23
123 1 35.23
123 2 25.23
456 0 25.23
From what I understand, the code should look something like:
select t.Building_ID, t.Sector_ID, t.Temperature, t.Temp_Date
from MyTable t
inner join (
select Building_ID, Sector_ID, max(Temp_Date) as MaxTemp_Date
from MyTable
group by Building_ID
) tm on t.Building_ID = tm.Building_ID and t.Sector_ID = tm.Sector_ID and t.Temp_Date = tm.Temp_Date
EDIT
Came back to it this morning and I believe the following code is getting me what I want
select t.Building_ID, t.Sector_ID, t.Temperature, t.Temp_Date
from MyTable t
inner join (
select Building_ID, Sector_ID, max(date_time) as maxMaxTemp_Date
from MyTable t
group by Building_ID, Sector_ID
) tm on t.Building_ID = tm.Building_ID and t.Sector_ID = tm.Sector_ID and t.Temp_Date=tm.MaxTemp_Date
ORDER BY t.Building_ID, t.Sector_ID