I have a table dbo.counters with columns idx, idx_device, DateTime, Data1, Data2 etc.
- idx is key and autoincremental
- idx_device refers to another table, hundreds of rows may exists in table with same idx_device value
- DateTime is TimeStamp when record has been written
I need to have from each idx_device one complete row which is closest to a specific DateTime, but not higher than.
I got already following formula, but this does not output all columns from table:
select max(DateTime), idx_device from dbo.counters
WHERE DateTime <= '02/04/12 23:59:59.995'
group by idx_device
guess should be something small, but I hang… thanks
Here is a solution using Common Table Expressions: