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
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.  ;)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.