Sophia said:
the earlier minutes wasn't capture since it depend on the date time i run the script.
I think by this you mean that if the current time is 11:14 Solution 1 will literally return everything within the last 2 hours i.e. from 9:14 to 11:14 but what you actually want is everything from 09:00 to 11:00.
If so then @RamiroX is close. Try
SELECT [LoadingDateTime]
FROM
@Products where
DATEDIFF(hour, [LoadingDateTime], getdate()) <= 2
group by [LoadingDateTime]
order by [LoadingDateTime] asc
One other point:
- Don't use
Select *
if using Group By - list the columns explicitly. To be honest, this is true in general for good coding practice.