Jump to content

get a running total for each day, between start date and end date


Recommended Posts

Hi Guys,

 

I am trying to get a running running total, or sub-total, for each day, and then a full total, for the dates selected..

 

My query works when I pass it, start date 3/03/2010 till 6/03/2010, it get the full total, but I dont no how to get the running total for each day..

 

All help is welcome

 

 

<?php
if($_POST['action']) {

$start_date = $_POST['start_date'];
$end_date = $_POST['end_date'];
$campaign_id = $_SESSION['campaign_id'];

$sql_get_data = "SELECT * FROM sold_leads WHERE deal_date BETWEEN '".$start_date."' AND '".$end_date."' ORDER BY deal_date ASC";
	$result_get_data = mysql_query($sql_get_data, $AngelG) or die(mysql_error());

	while($row_get_data = mysql_fetch_array($result_get_data)){
	if($row_get_data['sellers_company'] == $my_company){
	$total_credit_added = $total_credit_added + $row_get_data['price'];
	}
	else if($row_get_data['buyers_company'] == $my_company){
	$total_credit_spend = $total_credit_spend + ($row_get_data['price']);
	}

	$yeasterdays_total_spend = $row_get_data['price'];
	}

	echo $total_credit_added;
	echo "<br/>";
	echo "-" . $total_credit_spend;

}
?>

too lazy to read through all the code but if you didn't mind doing a query for each day, do something like this using mysql's SUM() function

 

$q = "SELECT SUM(price) AS DailyTotal FROM sold_leads WHERE deal_date = ".$specific_day"' ORDER BY deal_date ASC";
$r = mysqli_query($connection, $q);
$row = mysqli_fetch_array($q);
echo 'Daily total for ' . $specific_day . ' is ' . $row['DailyTotal'] . '.';

Assuming your deal_date column is an actual mysql DATE data type, you can use SUM(price) in the SELECT term and a GROUP BY deal_date term to give the sum for each day. You can also use a WITH ROLLUP term to give the total of all the sums.

 

If your deal_date column is actually storing values in the format - 3/03/2010, you will find that your query will only work for a very small set of dates.

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.