I am trying to create a chart where by i will display data from my scores table and that data must be per the total scores from each month. I am a novice when it comes to advanced sql queries. I have tried following some examples on SO but still haven't got it yet.
Below is the scores table
---------------------------------
id | marks | date_created
---------------------------------
1 | 100 | 2020-03-12
2 | 90 | 2020-08-25
3 | 100 | 2021-04-10
4 | 95 | 2021-06-20
5 | 99 | 2021-10-02
6 | 97 | 2021-10-02
What i want to do is make sure that when i display the chart, it will show scores from the past 12 months and zeros where those months don't have scores.
#Edit 3 The table below is just a visualisation of what i want to achieve. I dont have it yet in the database. I only have the scores table
---------------------------------
month | sum(marks)
---------------------------------
October | 0
September | 0
August | 0
July | 0
June | 95
May | 0
April | 100
March | 100
February | 0
January | 0
December | 0
November | 0
As you can notice in the table, i want the latest month to be what's sorted last How can i achieve this? Thanks
Edit #1
The marks from the different scores are summations of marks from total days in individual months. As you can notice on the scores table, there's a day with 2 score entries. In my totals i would like for those scores to all be computed for the month of October.
Edit #2
function get_last_12_month_scores(){
$db = Database::getInstance();
$mysqli = $db->getConnection();
$user_id = $_SESSION['user_id'];
$query_count = "SELECT SUM(`marks`) as `total` FROM `scores` WHERE `user_id`='$user_id';
$month_12_query_count =
"SELECT
SUM(IF(month = 'Jan', total, 0)) as 'Jan',
SUM(IF(month = 'Feb', total, 0)) as 'Feb',
SUM(IF(month = 'Mar', total, 0)) as 'Mar',
SUM(IF(month = 'Apr', total, 0)) as 'Apr',
SUM(IF(month = 'May', total, 0)) as 'May',
SUM(IF(month = 'Jun', total, 0)) as 'Jun',
SUM(IF(month = 'Jul', total, 0)) as 'Jul',
SUM(IF(month = 'Aug', total, 0)) as 'Aug',
SUM(IF(month = 'Sep', total, 0)) as 'Sep',
SUM(IF(month = 'Oct', total, 0)) as 'Oct',
SUM(IF(month = 'Nov', total, 0)) as 'Nov',
SUM(IF(month = 'Dec', total, 0)) as 'Dec',
SUM(total) as total_yearly
FROM (
SELECT DATE_FORMAT(date_added, '%b') as month, SUM($query_count as total
FROM scores
WHERE date_added <= now() and date >= Date_add(now(),interval - 12 month)
GROUP BY DATE_FORMAT(date_added, '%m-%Y'))) as sub";
$query_result = $mysqli->query($month_12_query_count);
echo $query_result;
}
With echo $query_result;
i echoed to see if $query_result
would give me an array such that i could be able to capture the individual values in the array by doing get_last_12_month_scores()[i]
in the Chart.js data values script. But it unfortunately didn't
It's at this that i realised i was either not aware what i was doing or had to just seek help to avoid wasting time.