0
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

我试图比较“数字”是相同值的两个月,我想找出价格之间的差异(如果有的话)。

使用上面的数据,我想比较第 11 个月和第 12 个月(或第 10 个月和第 11 个月,具体取决于用户选择的这些)。因此,对于第 11 个月和第 12 个月,奶酪的价格差异为 -1.7 美元,而苹果的价格差异为 -3 美元。这就是我想要做的,除了它应该对两个月之间在“数字”相同的表中找到的所有行执行此操作。

这是我当前的代码......它目前正在按项目名称而不是项目编号进行比较。然而,奇怪的是,无论出于何种原因,它只发现了 200 多个项目中的 3 个项目的差异。

感谢您的任何帮助。

<?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>
<?
}
?>
4

1 回答 1

1

怎么样的东西:

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;

如果我没有犯任何错误,则此查询应为您提供所需的信息:

(例如,montha = 10 和 monthb = 11)

希望这可以帮助。

于 2013-02-04T20:08:07.647 回答