Jump to content

[SOLVED] Incremental results


fragerdaz

Recommended Posts

My table:

field1: id (auto_increment)

field2: date

 

My query:

SELECT count(name), date FROM table GROUP BY date

 

This returns an array with each date and the number of occurences of that date.

 

My problem:

I need to increment each new date with the previous results.  Here's an example:

 

date | count(id) | what I need

------------------------------

2007-01-01 | 3 | 3    << 3 rows with a date of 2007-01-01

2007-04-02 | 7 | 10  << 7 rows with a date of 2007-04-02 (I need the current total of 10)

2007-04-06 | 5 | 15  << 5 rows with a date of 2007-04-06 (I need the current total of 15)

2007-07-28 | 1 | 16  << 1 row with a date of 2007-07-28 (I need the current total of 16)

and so on...

 

 

How can I accomplish this?

Link to comment
https://forums.phpfreaks.com/topic/66122-solved-incremental-results/
Share on other sites

Thanks for your replies, but none of the proposed solutions worked.  :-\

 

Here's how I got it working:

 

$query = "SELECT date, count(name) FROM table GROUP BY date";
if ($result = $mysqli->query($query)) {
	while ($obj = $result->fetch_assoc()) {
		$data[] = $obj;
	}
	$sum = "0";
	for ($i = 0; $i < count($data); $i++) {
		$sum += $data[$i]['count(name)'];
		$data[$i]['count(name)'] = $sum;
	}
	return $data;

 

This returns an array of distinct dates with the incremental number of rows for each date.  ;)

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.