So, its my original table. The duration is in second.
| Duration | Status |
|---|---|
| 30 | 0 |
| 30 | 0 |
| 31 | 1 |
| 30 | 1 |
| 31 | 1 |
| 31 | 0 |
| 31 | 1 |
| 30 | 0 |
| 30 | 0 |
| 30 | 0 |
| 31 | 0 |
| 30 | 1 |
| 30 | 1 |
| 30 | 0 |
and i want the result is something like this (if possible)
| Status | sum of duration |
|---|---|
| 0 | 60 |
| 1 | 92 |
| 0 | 31 |
| 1 | 31 |
| 0 | 121 |
| 1 | 60 |
| 0 | 30 |
i want to sum the duration for each status (0 and 1), i have try some query like sum, group, or where modification... but i dont get the correct one.
I hope someone can help me. Thank you in advance : )
note : i use mysql phpmyadmin im sorry, i just edited the result that i desire. because there is slight mistake in my manual count
CodePudding user response:
Remember that an RDBMS table is a set.
What you're showing in your question looks like an ordered list, rather than a set.
Write some code that reads
duration, status, and writes
duration, status, session_id.
Now, with those seven distinct IDs appearing in a table,
you're in a good position to use SUM(duration)
in a GROUP BY session_id query.
CodePudding user response:
Have you try combining SUM with CASE to achieve this
https://learnsql.com/blog/case-when-with-sum/
Since you have not provided the table structure here is a generic example
SELECT
first_name,
last_name,
score,
CASE
WHEN score > 90 THEN 'Exceptional result'
WHEN score > 70 THEN 'Great result'
WHEN score > 50 THEN 'Average result'
ELSE 'Poor result'
END AS score_category
FROM test_result
ORDER BY score DESC;
The CASE conditions can be quite complex and could help you achieve what you need.
