I'm trying to do a report for my company, and I have to show who used every item of the list last. To simplify, my table looks like this:
Item Code | Usage Date | User Name
00001 | 19/01/2020 | Person A
00001 | 05/01/2020 | Person A
00001 | 23/12/2019 | Person B
00002 | 09/01/2020 | Person C
etc
Now, my problem is the following: Sometimes, there is no user specified, making the table look more like this:
Item Code | Usage Date | User Name
00001 | 19/01/2020 |
00001 | 05/01/2020 | Person A
00001 | 23/12/2019 | Person B
00002 | 09/01/2020 |
etc
And I have to GROUP my query BY Item Code with the latest date, but also the latest user that isn't empty, aka I want the final table to look like this:
Item Code | Usage Date | User Name
00001 | 19/01/2020 | Person A
00002 | 09/01/2020 |
etc
What I've tried:
- A simple GROUP BY [User Name]
- A temporary table with only the Item Code and the Usage Date, and JOINing the User Name afterwards
- A nested SELECT statement to do the GROUP BY after the ORDER BY
May you please give me directions as to how I can solve this problem?