Well, lucky for you this is what databases are born to do. The SQL language (which is generally uniform but differs a bit from vendor to vendor) has what are called Aggregate Functions, such as MAX, MIN, and most relevant to you, SUM.
If I did a query like thisā¦
SELECT SUM(weight) FROM weight_record
Iād get back the sum of the weights for all time.
But, you want to break it up by day. Thatās what SQLās āGROUP BYā clause is for. It lets you break up the datasets that aggregate functions apply to.
For the sake of simplicity, imagine we had a column in that weight table called ādayā, which had the day of the month for that record in it. We could do the followingā¦
SELECT day, SUM(weight) FROM weight_record GROUP BY day
and end up with the sum for each day. Trouble is, you probably donāt have that column. Instead, each row has a timestamp. So, what you need to do is find a way to use just the date portion of the whole time.
How you do that will depend on the database system youāre using, since youāll be using a function provided by the db system instead of the sql language. Letās assume youāre using MySQL. Going over to the MySQL Date/Time Function Reference Page we find thereās a function called Date that will extract just the date from a timestamp.
So, using that, we end up with:
SELECT DATE(t_stamp), SUM(weight) FROM weight_record GROUP BY DATE(t_stamp)
and VOILA, you end up with the weights by day. Clean it up a bit (mainly sort the data), and itās ready to go in the graph:
SELECT DATE(t_stamp) as 'dt', SUM(weight) FROM weight_record GROUP BY dt ORDER BY dt ASC
(Note: I also gave the column an ALIAS to make things a bit easier to read and work with).
Hope that helps. Basically, I wanted to show you how to do it, but also give you some vocab as a starting point for looking up more about SQL on your own. Main points: aggregate functions, group by clause, and the order by clause.
Regards,