Background
Piggybacking on a previous question I have a table with these sample values:
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
Currently, I am running this SQL query to get the latest value for each sector:
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
Learning more about SQL, I know that I can:
Add a
WHERE Building_ID = [I32 value]
to limit the values returned to one Building_ID valueTurn this into a stored procedure
show-temps
allowing my server to more efficiently return the desired filtered data with the statement `show-temps([value]).
Doing both of these would get me something that looks like
CREATE PROCEDURE show-temps
@Building int,
AS
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
WHERE
(t.Building_ID = @Building)
ORDER BY
t.Building_ID, t.Sector_ID
My question: is there a way to pass an array of int values to the WHERE()
function so that I could get the latest data for a set of buildings instead of calling the function multiple times?