I have a results table which lists a set of values, each linking to another table containing the date that result was made.
I have working SQL to get all the dates (using CASE’s) however I can only retrieve a single range of results.
Select
count(CASE
WHEN results.test_id IN ( SELECT id
FROM `test`
WHERE `posted`
BETWEEN '2011-07-01 00:00:00'
AND '2011-07-01 23:59:59')
THEN results.test_id
ELSE NULL
END) AS "1st July"
from `results`
WHERE results.window_id = 2 and results.mark > 90;
I also have another SQL query which gets all the ranges but can only work for one date at a time.
SELECT
CASE
when mark > 90 then '>90%'
when mark > 80 then '>80%'
when mark > 70 then '>70%'
END as mark_results,
COUNT(*) AS count
FROM (SELECT mark from results where window_id =2) as derived
GROUP BY mark_results
ORDER BY mark_results;
What I’d like is to have everything in one unified query, displaying the relevant totals for each range of results. such as below:
Result Range | 1st July | 2nd July | 3rd July | 4th July
>90% | 0 | 0 | 0 | 1
>80% | 1 | 2 | 1 | 1
>70% | 4 | 5 | 5 | 4
So that the totals for each range are displayed under their date.
I assume it’s possible.
The following statement joins results and tests in the FROM clause. It then aggregates the query by the mark range, with the counts per day:
Just add whatever days you want to the SELECT clause.
I should add . . . if you want all the dates, you need to put them on separate rows:
In fact, you might consider having a separate row for each date, with the ranges pivoted as columns.