I found this article helpful: How to select only the first rows for each unique value of a column?
However, it did not fully cover my requirements. To be honest, I don't think I fully understood the purpose / how to use the partition statement.
Starting with this MS SQL Table, I'd like to select the latest date for each File/Device combination.
+-----------------------+------------------------+------------------------+
| Date | File | Device |
+-----------------------+------------------------+------------------------+
| 09/09/2022 | Acrobat.exe | ABC123 |
| 09/10/2022 | Acrobat.exe | DEF456 |
| 11/07/2021 | Visio.exe | ABC123 |
| 12/31/2020 | Acrobat.exe | ABC123 |
+-----------------------+------------------------+------------------------+
I wrapped my head around this for a while now and the only thing I could come up with was
with cte as (
SELECT Date, File, Device,
ROW_NUMBER() over (partition by Device order by File, Date DESC) as [r]
FROM MyTable
)
select Date, File, Device, from cte WHERE [r] = 1
However, this will not always return exactly what I need. I'm not quite sure why but I think because it's only taking the change of one column into consideration for the row number to increase it's value.