'SQL GROUP BY with a custom order
I have a database table with answers for different questions. Now I need an evaluation for this table and I want to perform it in SQL.
The answer table with some data:
| id | item_id | user_id | percent | input_id | weekday | week | list_id | date | prio |
|---|---|---|---|---|---|---|---|---|---|
| 21580 | 517 | 1 | 0 | 1058 | Mo | 2022-03 | 23 | 2022-01-17 | 1 |
| 845 | 517 | 16 | 100 | 1057 | Mo | 2022-03 | 23 | 2022-01-17 | 2 |
| 21641 | 517 | 1 | 100 | 1057 | Di | 2022-03 | 23 | 2022-01-18 | 1 |
| 943 | 517 | 33 | 100 | 1057 | Di | 2022-03 | 23 | 2022-01-18 | 3 |
| 21766 | 517 | 1 | 0 | 1058 | Mi | 2022-03 | 23 | 2022-01-19 | 1 |
| 1005 | 517 | 33 | 100 | 1057 | Mi | 2022-03 | 23 | 2022-01-19 | 3 |
| 21703 | 517 | 1 | 100 | 1057 | Do | 2022-03 | 23 | 2022-01-20 | 1 |
| 1072 | 517 | 33 | 100 | 1057 | Do | 2022-03 | 23 | 2022-01-20 | 3 |
| 21772 | 517 | 1 | 0 | 1058 | Fr | 2022-03 | 23 | 2022-01-21 | 1 |
| 1107 | 517 | 33 | 100 | 1057 | Fr | 2022-03 | 23 | 2022-01-21 | 3 |
| 1162 | 517 | 33 | 0 | 1058 | Sa | 2022-03 | 23 | 2022-01-22 | 3 |
| 1223 | 517 | 1 | 100 | 1057 | So | 2022-03 | 23 | 2022-01-23 | 8 |
I used the following query to get those results.
SELECT *
FROM answer
WHERE item_id = 517
AND week = '2022-03'
ORDER BY date asc, prio asc;
Now I need the weekdays only once in that order that is shown above. I tried to use GROUP BY but it did not work in that way I need it. So what I need is this:
| id | item_id | user_id | percent | input_id | weekday | week | list_id | date | prio |
|---|---|---|---|---|---|---|---|---|---|
| 21580 | 517 | 1 | 0 | 1058 | Mo | 2022-03 | 23 | 2022-01-17 | 1 |
| 21641 | 517 | 1 | 100 | 1057 | Di | 2022-03 | 23 | 2022-01-18 | 1 |
| 21766 | 517 | 1 | 0 | 1058 | Mi | 2022-03 | 23 | 2022-01-19 | 1 |
| 21703 | 517 | 1 | 100 | 1057 | Do | 2022-03 | 23 | 2022-01-20 | 1 |
| 21772 | 517 | 1 | 0 | 1058 | Fr | 2022-03 | 23 | 2022-01-21 | 1 |
| 1162 | 517 | 33 | 0 | 1058 | Sa | 2022-03 | 23 | 2022-01-22 | 3 |
| 1223 | 517 | 1 | 100 | 1057 | So | 2022-03 | 23 | 2022-01-23 | 8 |
What I get when I use GROUP BY weekday is this:
SELECT *
FROM answer
WHERE item_id = 517 AND week = '2022-03'
GROUP BY weekday
ORDER BY date asc, prio asc```
| id | item_id | user_id | percent | input_id | weekday | week | list_id | date | prio |
|---|---|---|---|---|---|---|---|---|---|
| 845 | 517 | 16 | 100 | 1057 | Mo | 2022-03 | 23 | 2022-01-17 | 2 |
| 943 | 517 | 33 | 100 | 1057 | Di | 2022-03 | 23 | 2022-01-18 | 3 |
| 1005 | 517 | 33 | 100 | 1057 | Mi | 2022-03 | 23 | 2022-01-19 | 3 |
| 1072 | 517 | 33 | 100 | 1057 | Do | 2022-03 | 23 | 2022-01-20 | 3 |
| 1107 | 517 | 33 | 100 | 1057 | Fr | 2022-03 | 23 | 2022-01-21 | 3 |
| 1162 | 517 | 33 | 0 | 1058 | Sa | 2022-03 | 23 | 2022-01-22 | 3 |
| 1223 | 517 | 1 | 100 | 1057 | So | 2022-03 | 23 | 2022-01-23 | 8 |
This is the ruleset for the evaluation:
I need the newest answer based on the priority of that answer.
Every user can have for every list a specific priority. 1 is the highest and 10 (default) the lowest.
So if 10 users answer the same question at the same day, I only need the newest answer with the lowest integer value form the prio column. That's why I ORDERY BY prio ASC.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
