Jump to content

Grouping count by month


Recommended Posts

I'm trying to get sales counts by month but having issues. Here is the dump

array(2) { [0]=> object(stdClass)#80 (2) { ["Counter"]=> int(61) ["Month"]=> NULL } [1]=> object(stdClass)#81 (2) { ["Counter"]=> int(5) ["Month"]=> int(5) } }

how would i get

{ ["Counter"]=> int(61) ["Month"]=> int(5) }

Here is the code i am using

MySQL
 

public function get_sales_list() {
	return $this->db->select("SELECT * FROM ".PREFIX."tservices WHERE InGraph = '1'");
}
public function get_sales_count($id) {
	return $this->db->select("SELECT COUNT(".PREFIX."tprojects.IdProject) as Sales
							  FROM 
										   ".PREFIX."tprojects 
							  WHERE 
										   ".PREFIX."tprojects.IsActive = '1'
							  AND 
										   ".PREFIX."tprojects.Service = '$id'
							  GROUP BY 
							  MONTH(".PREFIX."tprojects.Start), YEAR(".PREFIX."tprojects.Start)");
}

PHP
 


$servGraph =  $this->_query->get_sales_list();

if(isset($servGraph)) {
	$sales = array();
	foreach($servGraph as $row) 
	{
		$count = $this->_query->annual_sales($row->IdService);
		foreach($count as $key => $val)
		{
			array_push($sales, $val);
		}
	}
}
$data['sales-list'] = $sales;

var_dump($data['sales-list']);

 

Thanks in advance for any help or suggections

Link to comment
Share on other sites

54 minutes ago, Barand said:

To get the month in your results you need to select it. Your query only selects the count.

That is the issue because this also give the same result
 


	public function get_sales_count($id) {
		return $this->db->select("SELECT COUNT(".PREFIX."tprojects.IdProject) as Sales,
									     MONTH(".PREFIX."tprojects.Start) AS Month
								  FROM 
										 ".PREFIX."tprojects 
								  WHERE 
										 ".PREFIX."tprojects.IsActive = '1'
								  AND 
										 ".PREFIX."tprojects.Service = '$id'
								  GROUP BY 
    									 MONTH(".PREFIX."tprojects.Start), YEAR(".PREFIX."tprojects.Start)");
	}
array(2) { [0]=> array(2) { ["Count"]=> int(61) ["Month"]=> NULL } [1]=> array(2) { ["Count"]=> int(5) ["Month"]=> int(5) } }

I cannot understand where thus is coming from either because all entries are may 2024

Quote

 array(2) { ["Count"]=> int(5) ["Month"]=> int(5) }

 

Link to comment
Share on other sites

9 minutes ago, Andy2024 said:
array(2) { [0]=> array(2) { ["Count"]=> int(61) ["Month"]=> NULL } [1]=> array(2) { ["Count"]=> int(5) ["Month"]=> int(5) } }

That seems to suggest you have 61 entries with an invalid date (null) and 5 entries for month 5 (may).

I don't understand where "Count" index is coming from in that results array as the count() has an column alias "Sales".

Link to comment
Share on other sites

i think it may be returning them on different rows in the array

 

Quote

array(2) { [0]=> array(2) { ["Count"]=> int(61) ["Month"]=> NULL } [1]=> array(2) { ["Count"]=> int(5) ["Month"]=> int(5) } }

[0]=> array(2) { ["Count"]=> int(61) ["Month"]=> NULL }

[1]=> array(2) { ["Count"]=> int(5) ["Month"]=> int(5) } }

[0]is returning the count and [1] is returning the month (5) May!

Link to comment
Share on other sites

Sorted thanks

I ended up  putting


									if(!empty($counts->Counter) && !empty($counts->Month)) {
										array_push($sales, array('Count' => $counts->Counter, 'Month' => $counts->Month));
									}

At least its giving the correct count

Link to comment
Share on other sites

I was going to suggest

mysql> select * from tprojects;
+-----------+---------+----------+------------+
| IdProject | service | IsActive | start      |
+-----------+---------+----------+------------+
|         6 |     123 |        1 | 2024-05-01 |
|         7 |     123 |        1 | 2024-05-02 |
|         8 |     123 |        1 | 2024-05-03 |
|         9 |     123 |        1 | 2024-05-04 |
|        10 |     123 |        1 | 2024-05-05 |
|        11 |     123 |        1 | NULL       |
|        12 |     123 |        1 | NULL       |
|        13 |     123 |        1 | NULL       |
|        14 |     123 |        1 | NULL       |
|        15 |     123 |        1 | NULL       |
|        16 |     123 |        1 | NULL       |
|        17 |     123 |        1 | NULL       |
+-----------+---------+----------+------------+


mysql> SELECT COUNT(IdProject) as Sales
    ->      , DATE_FORMAT(start, '%M %Y') AS Month
    -> FROM tprojects
    -> WHERE IsActive = '1'
    ->       AND Service = 123
    ->       AND MONTH(start) IS NOT NULL
    -> GROUP BY month;
+-------+----------+
| Sales | Month    |
+-------+----------+
|     5 | May 2024 |
+-------+----------+

 

Link to comment
Share on other sites

  • 3 weeks later...

I have had to go about it a totally different way because I need figures for each job type so have got this

 

"SELECT ".PREFIX."tservices.IdService,
	".PREFIX."tservices.Name,
	".PREFIX."tservices.GraphColor,
	".PREFIX."tprojects.Service,
COUNT(".PREFIX."tprojects.IdProject) as Sales,
	".PREFIX."tprojects.Finish
FROM 		 
	".PREFIX."tservices
LEFT JOIN ".PREFIX."tprojects ON ".PREFIX."tservices.IdService = ".PREFIX."tprojects.Service

WHERE
	".PREFIX."tservices.InGraph = '1'
GROUP BY MONTH(".PREFIX."tprojects.Finish), YEAR(".PREFIX."tprojects.Finish)
ORDER BY ".PREFIX."tprojects.Finish DESC LIMIT 0, 12"

Then the PHP is

 


if(!empty($data['legend'])) {
  foreach($data['legend'] as $r1) {
  	if(isset($r1->Name)) {
      $sales = $this->_query->get_sales_count($r1->IdService);
      if(!empty($sales)) {
          foreach($sales as $r2) {
          $count[] = $r2->Sales; 
      }
      $legend[] = array('label' => $r1->Name,
      'backgroundColor' => $r1->GraphColor,
      'data' => implode(',',$count));
      }

    }
  }
}

But the json its outputting

[{"label":"Installation","backgroundColor":"#2bca2d","data":"8,19,11,3,49"},{"label":"Repair","backgroundColor":"#cfca3a","data":"8,19,11,3,49,1,3,7"}]

instead of

[{"label":"Installation","backgroundColor":"#2bca2d","data":"8,19,11,3,49"},{"label":"Repair","backgroundColor":"#cfca3a","data":"1,3,7"}]

Does anyone have any idea how to solve this?

Thanks in advance

Link to comment
Share on other sites

Bit of a breakthrough

I've altered the PHP to


				
				if(!empty($data['legend'])) {
					foreach($data['legend'] as $r1) {
						if(isset($r1->Name)) {
							$sales = $this->_query->get_sales_count($r1->IdService);
							if(!empty($sales)) {
								foreach($sales as $r2) {
									$count[$r1->Name][] = $r2->Sales; 
								}
								$legend[] = array('label' => $r1->Name,
																  'backgroundColor' => $r1->GraphColor,
																  'data' => implode(',',$count[$r1->Name]));
							}
							
						}
					}
				}

 

and that has given me

[{"label":"Installation","backgroundColor":"#2bca2d","data":"8,19,11,3,49"},{"label":"Repair","backgroundColor":"#cfca3a","data":"1,3,7"}]

which looks ok until its placed in the char then it adds everything to the wrong month and only 1 item per month

download.png

Link to comment
Share on other sites

I have no idea how your chart software works but if you provide five data values (8, 19, 11, 3, 49) for one type and three data values (1, 3, 7) for the other, how is it supposed to know which months those values are for?

You have a very weird x-axis for that chart (the month sequence is 6-5-4-3-2-1-12-11-10-9-8-7). The norm is to put them in chronological order.

  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.