Solved

MYSQL PHP: How do I calculate percentage change from mysql table?

Posted on 2008-06-11
14
2,205 Views
Last Modified: 2013-12-13
MYSQL PHP

Hello,

I have a MySQL table with 2 fields:

[ Date ] [ Value ]
2008-06-11 2200
...
2008-05-11 2000


I would like to calculate the percentage increase or decrease as at the last date (e.g. 2007-12-31), when compared to 1 month ago.

So in the example, I want to calculate (2200-2000) / 2000

How do I write this in a mysql query in a PHP script?


Thanks.
0
Comment
Question by:gingera
  • 8
  • 6
14 Comments
 
LVL 9

Accepted Solution

by:
Rob Siklos earned 500 total points
ID: 21762713
Assuming there is only one record for each day, you should be able to do something like this:
$date = "2008-06-11";
 
$sql = "SELECT (t1.value - t2.value)/t2.value AS result
FROM table t1
LEFT JOIN table t2
ON t2.`date` = DATE_SUB(t1.`date`, INTERVAL 1 MONTH)
WHERE t1.`date` = '$date'";

Open in new window

0
 

Author Comment

by:gingera
ID: 21763029
Thanks Rob. Hmmm... I yielded no result with your query.

i.e. when I

echo "$result";

nothing appeared.
0
 
LVL 9

Expert Comment

by:Rob Siklos
ID: 21763071
I just tested it and it worked for me.  Can you post the PHP code you're using to execute the query and print the result?
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 9

Expert Comment

by:Rob Siklos
ID: 21763082
Oh, also, for robustness I would change the LEFT JOIN to INNER JOIN
0
 

Author Comment

by:gingera
ID: 21763169
OK this is the code I tried

Table name: rice

Table fields: date, close
<?php
 
$connection = mysql_connect('localhost', 'username', 'password');
$db = mysql_select_db('prices', $connection);
$query_percentage_change = 	"SELECT ((t1.`close` - t2.`close`)/t2.`close`) AS percentage_change
					FROM `rice` t1
					INNER JOIN `rice` t2
					ON t2.`date` = DATE_SUB(t1.`date`, INTERVAL 1 MONTH)
					WHERE t1.`date` = (SELECT MAX(`date`) FROM `rice`) ";
	$result_percentage_change = mysql_query($query_percentage_change);
	while ( $row = mysql_fetch_array($result_percentage_change) )
	{
			extract($row);
			echo "$percentage_change";
	}
?>

Open in new window

0
 

Author Comment

by:gingera
ID: 21763187
I got error message

PHP Warning:  mysql_fetch_array(): supplied argument is not a valid MySQL result resource
0
 
LVL 9

Expert Comment

by:Rob Siklos
ID: 21763201
before "extract($row);" can you add "print_r($row);" and see what gets printed?
0
 

Author Comment

by:gingera
ID: 21763260
OK, did that. Nothing displayed.
0
 
LVL 9

Expert Comment

by:Rob Siklos
ID: 21763263
Seems like there's something wrong with the result of the call to mysql_query().

After that line, try adding this:

if (!$result_percentage_change) {
    $message  = 'Invalid query: ' . mysql_error() . "\n";
    die($message);
}
0
 

Author Comment

by:gingera
ID: 21763294
OK I have a possible explanation.

Maybe there is no data for the date exactly 1 month before. For example, a weekend?

If that is the case, how can we find the next closes date to 1 month (if possible)?
0
 

Author Comment

by:gingera
ID: 21763340
OK I changed "interval 1 month" to "interval 2 month" and a result is displayed.

So the problem is as I have described above.

How do I solve this problem (no data for exactly 1 month ago)?
0
 
LVL 9

Expert Comment

by:Rob Siklos
ID: 21763355
Ok, that is a different problem for sure.

At first thought, I would say that you need a routine in PHP to select the date you want to compare to.
0
 

Author Comment

by:gingera
ID: 21763376
OK I am closing this question and allocating the points to you Rob. Thanks very much for your help.

I will start another question. THANKS!
0
 

Author Closing Comment

by:gingera
ID: 31466259
Thanks for your patience for this newbie.
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

823 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question