Jump to content

finding the average of SQL results


busby

Recommended Posts

Hi

 

I hope this is a simple problem that i cant solve and someone here will know.

 

I need to create an SQL report showing average sales figures for a period of time decided by the user. for example if £100 was made in 10 days the average figure would be £10 a day.

 

Ive created a page where a user enters in 2 dates. SQL then selects the nett_value, date, order numbers etc from an orders table where the dates are between the 2 dates given.

 

I need to find the average nett_value from all those returned. I know i have to add together all the nett prices and then divide by how many dates there are but i dont know how to do that in the code. Because if there are several purchases in 1 day all of the purchases are counted but the day only gets counted once. or miss the day completely if there were zero purchases.

 

So i used the SELECT SUM(nett_value) function for selectin all of the figures and adding them together...now i need to know how to divide them by how many days there are between the 2 entered dates.

 

can someone help me?

Link to comment
Share on other sites

The SQL is

 

	$sql  = 'SELECT order_id, order_num, date, SUM(nett_price) ';
	$sql .= 'FROM orders ';
	$sql .= 'WHERE complete = "Y" AND tx_type = "PAYMENT" AND date >= "'.$_POST['start_date'].'" AND date <= "'.$_POST['finish_date'].'" ';

 

Lets assume i test it and type in 2 dates and it returns ten results.

 

thats 10 orders. however there is only 7 days because 3 orders were made in 1 day. so i need to add all the 10 nett_prices together and divide by the 7 days.

 

if that makes sense.

 

but i dont know how to do that in the code.

Link to comment
Share on other sites

One thing i thought of but wasnt sure if it was correct was.....

 

Could i just do another SQL statement underneath the one i already have only selecting the DISTINCT dates within that date range?

 

Then dividing the SUM of the nett values by the amount of dates returned from the second SQL statement?

 

would that work?

 

Any help is greatly appreciated.

Link to comment
Share on other sites

howas about keeping it all together?:

 

        	$sql  = 'SELECT order_id, order_num, date, SUM(nett_price) as total, COUNT(DISTINCT date) as num_days, (ROUND(SUM(nett_price) / COUNT(DISTINCT date)), 2) AS daily_avg ';
	$sql .= 'FROM orders ';
	$sql .= 'WHERE complete = "Y" AND tx_type = "PAYMENT" AND date >= "'.$_POST['start_date'].'" AND date <= "'.$_POST['finish_date'].'" ';

Link to comment
Share on other sites

Ive never seen an SQL statement like that before. im a rookie with this kind of stuff but i tried inserting it into my code and i couldnt get it to work.  Would the method i mentioned above work? having the extra SQL statement below the main one?

 

 

Link to comment
Share on other sites

well...i swapped my SQL statement for the one you gave me, then I commented out everything else below that statement and just had the following.

 

		
$db_orders_query_result = mysql_query($sql, $db_connection);
print($db_orders_query_result['daily_avg']);

 

i ran the page but nothing shows up...no errors or anything..just blank

Link to comment
Share on other sites

ok...ive spotted a flaw in all of this. even if i got this SQL to work it wouldnt give me the average sales figures for each day. because it could occurr that some days may have zero orders therefore wont appear in the database orders table. so if someone types in september 1st and september 10th...but september the 5th didnt have any orders it would only be counting 9 days and not 10...giving the wrong result.

 

so.......back to square one....see original post lol :)

Link to comment
Share on other sites

hmm, give this a shot then:

$start = $_POST['start_date'];
$end = $_POST['finish_date'];
$sql  = "SELECT order_id, order_num, date, SUM(nett_price) as total, (SUM(nett_price) / DATEDIFF('$end', '$start')) AS daily_avg FROM orders WHERE ((complete = 'Y') AND (tx_type ='PAYMENT') AND (date BETWEEN '$start' AND '$end'))";

also update youre or die() to read:

or die("ERROR: >> <BR>".mysql_error()."<BR> this occured trying to run the following query: <BR> $sql");

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.