Let's say I've just carried out this query upon a table to achieve this output:
QUERY:
SELECT staff_id, tablea_A.item_id, item_type, status, date_installed
FROM table_A
INNER JOIN table_B ON table_A.item_id = table_B.item_id
Group By staff_id, tablea_A.item_id, item_type, status, date_installed
OUTPUT:
staff_id | item_id | item_type | status | date_installed |
---|---|---|---|---|
1 | S4193 | Ball | Active | 17/09/2021 |
1 | S5567 | Ball | Active | 22/07/2021 |
4 | S9088 | Ball | Reserved | 21/12/2021 |
7 | J8877 | Club | Active | 03/03/2016 |
..etc
Now that I've created a table which is grouped primarily by staff_id, I would like to display a single item per staff member, by prioritising this criteria in this order:
a. balls over any other item
b. active status over any other status
c. most recently installed items if date is provided
I've read about using the HAVING clause, but I wouldn't know how to implement it in this sense with a particular criteria. Any help would be hugely appreciated.