Home > Software design >  Working with MySQL / MySQLi data - daily, weekly and monthly graphs
Working with MySQL / MySQLi data - daily, weekly and monthly graphs

Time:11-07

I'm developing a dashboard with graphs.

What's the problem?
Let's say, that I have a table with the folowing sctructure:

 ------- ------ ------ ----- --------- ------- 
| Field | Type | Null | Key | Default | Extra |
 ------- ------ ------ ----- --------- ------- 
| total | int  | NO   |     | NULL    |       |
| new   | int  | NO   |     | NULL    |       |
| date  | date | YES  |     | NULL    |       |
 ------- ------ ------ ----- --------- ------- 

where total stands for Total Members and new for New Members (date is a date of course - in format: yyyy-mm-dd).

Example of columns:

 ------- ------- ------------ 
| total | new   | date       |
 ------- ------- ------------ 
|  3450 |    21 | 2021-11-06 |
|  3650 |   200 | 2021-11-07 |
|  3694 |    34 | 2021-11-08 |
|  3520 |    26 | 2021-11-09 |
|  3399 |  -321 | 2021-11-10 |
|  3430 |    31 | 2021-11-11 |
|  3450 |    20 | 2021-11-12 |
|  3410 |   -40 | 2021-11-13 |
|  3923 |   513 | 2021-11-14 |
|  4019 |    96 | 2021-11-15 |
|  4119 |   100 | 2021-11-16 |
|  4000 |  -119 | 2021-11-17 |
|  3000 | -1000 | 2021-11-18 |
|  3452 |   452 | 2021-11-19 |
|  3800 |   348 | 2021-11-20 |
|  3902 |   102 | 2021-11-21 |
|  4050 |   148 | 2021-11-22 |
 ------- ------- ------------ 

And there are a few options, where the dashboard user can select between 2 dates and type of graphs (daily, weekly, monthly).

Dashboard overview

Image, that describes the Setting options.

The Point

I need to take these 2 dates and somehow get all data from the database between the given term. Well, but that's not all. The Daily, Weekly and Monthly option means, that graphs will be showing average newcoming and total members per every week (so if I will grab 7 days from the database, I need to create an average - and do this between all these days / weeks / months in a term), if it's weekly, etc. So the final graph will be showing something like:

 250 new           20 new           31 new
1000 total       1020 total       1051 total
  Nov 7            Nov 14           Nov 21

etc...

More informations:

Ubuntu: 21.04
MySQL: 8.0.27
PHP: 7.4.23
Apache: 2.4.46

Feel free to ask.
Does anyone have any ideas, please?
Thanks for the responses,
Adalbert

CodePudding user response:

I don't get where your numbers come from

But your query would go like this.

For the month you need to group by MONTHof course

CREATE TABLE members (
  `total` INTEGER,
  `new` INTEGER,
  `date` date
);

INSERT INTO members
  (`total`, `new`, `date`)
VALUES
  ('3450', '21', '2021-11-06'),
  ('3650', '200', '2021-11-07'),
  ('3694', '34', '2021-11-08'),
  ('3520', '26', '2021-11-09'),
  ('3399', '-321', '2021-11-10'),
  ('3430', '31', '2021-11-11'),
  ('3450', '20', '2021-11-12'),
  ('3410', '-40', '2021-11-13'),
  ('3923', '513', '2021-11-14'),
  ('4019', '96', '2021-11-15'),
  ('4119', '100', '2021-11-16'),
  ('4000', '-119', '2021-11-17'),
  ('3000', '-1000', '2021-11-18'),
  ('3452', '452', '2021-11-19'),
  ('3800', '348', '2021-11-20'),
  ('3902', '102', '2021-11-21'),
  ('4050', '148', '2021-11-22');
SELECT `new`,sumtotal, `date` FROM members m
INNER JOIN (SELECT  SUM(`new`) sumtotal, MIN(`date`) mindate  FROM members GROUP BY WEEK(`date`)) t1
ON m.`date`= t1.mindate
WHERE m.`date` BETWEEN '2021-11-07' AND '2021-11-22'
new | sumtotal | date      
--: | -------: | :---------
200 |      -50 | 2021-11-07
513 |      390 | 2021-11-14
102 |      250 | 2021-11-21

db<>fiddle here

  • Related