Reputation: 206
month year customer distributor number name price
10 2012 20 8 2406163 CHEESE 50.4
10 2012 20 8 2325141 APPLE 25.2
11 2012 20 8 2406163 CHEESE 48.1
11 2012 20 8 2325141 APPLE 20.2
12 2012 20 8 2325141 APPLE 23.2
12 2012 20 8 2406163 CHEESE 46.4
I am attempting to compare two months where "number" is the same value, I'd like to find the difference between the price (if any).
Using the data above, I would want to compare months 11 & 12 (or 10 and 11, depending of these are chosen by the user). So, for months 11&12, price difference for cheese is -$1.7 and apple is -$3. This is what I am trying to do, except it should be doing this for ALL rows found in the table between two months where "number" is the same.
Here is my current code...it is currently comparing by item name rather than item number. However, for whatever reason it only finds the difference for 3 items out of 200plus items, strangely.
Thanks for any assistance.
<?PHP
$from_date = $from_month;
$to_date = $to_month;
$query = "
select * from ogi
where month BETWEEN '" . $from_date . "' AND '" . $to_date . "' GROUP BY number HAVING count(*) > 1 ;
";
try
{
// These two statements run the query against your database table.
$stmt = $db->prepare($query);
$stmt->execute();
}
catch(PDOException $ex)
{
// Note: On a production website, you should not output $ex->getMessage().
// It may provide an attacker with helpful information about your code.
die("Failed to run query: " . $ex->getMessage());
}
// Finally, we can retrieve all of the found rows into an array using fetchAll
$rows = $stmt->fetchAll();
//Create a variable to hold the "previous month" values
$previousname = '';
$previousprice = '';
//Check each record from your query
foreach($rows as $row) {
//If not first time around && same item name as previous
if($previousname != '' && $previousname == $row['name']) {
//subtraction calculation here
if ($row['price'] <= $previousprice) {
$difference = "(Price Lowered) Price difference of $";
$result = $previousprice - $row['price'];
$percent = round(100.0*($previousprice/$row['price']-1));
?>
<tr>
<td><?php echo "" . $row['name'] . ""?></td>
<td><?php echo $difference; ?><?php echo $result ?> </td>
<td><?php echo $percent; ?> %</td>
</tr>
<?
} elseif ($row['price'] > $previousprice) {
$result = $row['price'] - $previousprice;
$percent = round(100.0*($previousprice/$row['price']-1));
$addition = "(Price Higher) Price difference of $";
?>
<tr>
<td><?php echo "" . $row['name'] . ""?></td>
<td><?php echo $addition; ?><?php echo $result ?> </td>
<td><?php echo $percent; ?>%</td>
</tr>
<?
}
}
else {
?>
<!-- <tr>
<td><?php echo "" . $row['name'] . ""?></td>
<td></td>
<td></td>
</tr> -->
<?
}
//Assign the "previous month" value
$previousname = $row['name'];
$previousprice = $row['price'];
}
?>
<?
}
else {
?>
<form action="" method="post">
Choose Months to Compare:<br>
Customer: <input type="text" name="customer"><br>
Month 1: <input type="text" name="from_month"><br>
Month 2: <input type="text" name="to_month"><br>
<input type="submit" value="Compare">
</form>
<?
}
?>
Upvotes: 0
Views: 1954
Reputation: 808
What about something like:
SELECT a.number, a.name, (a.price - b.price) as pdiff
FROM table a, table b
WHERE a.number = b.number
AND a.month = montha
AND b.month = monthb;
If I didn't make any mistake this query should give you the needed info:
(where montha = 10 and monthb = 11 for example)
Hope this helps.
Upvotes: 1