It sounds like you're using Excel to run a query in SQL to get the data you need. If you want to filter the data that comes back from SQL to just now plus previous hour (so 2 rows) then that becomes part of your WHERE
clause in your SQL query.
You can set this up a couple of ways. SQL itself can do the calculation if you are sure this is the only range you want.
SELECT ... FROM table WHERE [Date] = GETDATE() AND [Hour] BETWEEN CONVERT(INT, SUBSTRING(CONVERT(VARCHAR(20), GETDATE(), 114),1,2))-1 AND CONVERT(INT, SUBSTRING(CONVERT(VARCHAR(20), GETDATE(), 114),1,2))
Based upon your example, the challenge here is that you are using 24 hour time but GETDATE
will use whatever time format is the default which is most likely 12 hour time. So you have to convert to 24 hours. The above SUBSTRING/CONVERT
code takes the current date, gets the 24-hour time out of it and converts it back to a number. The result is all rows that are on the current date and between last hour and now, inclusive.
Alternatively you can do this on the Excel side. This is probably easier and especially better if you need to be able to change the range later. To do that add 2 parameters to your query in Excel: the begin hour and the end hour. Then adjust the SQL query to use the parameters instead of the code above. This would eliminate the SUBSTRING/CONVERT logic.
Note that you could possibly optimize the SQL query by combining the date and hour columns into a single DATETIME value and then your query would become quite a bit simpler. as you could just use BETWEEN. Something like this perhaps.
SELECT [Date], Hour, CONVERT(DATETIME, DATEADD(hour, Hour, [Date]))
FROM @table
WHERE CONVERT(DATETIME, DATEADD(hour, Hour, [Date])) BETWEEN DATEADD(hour, -1, GETDATE()) AND GETDATE()
For this to work your Date
column would have to be just a date with no time.