按月分组和添加结果

时间:2014-08-07 09:18:54

标签: php mysql

我已加入包含以下字段的表格:

company   invoicedate   amtdue
-------   -----------   ------ 
ABC       2014-07-20    100
DEF       2014-06-20    200 
ABC       2014-08-06    300 
GHI       2014-08-01    400  
GHI       2014-08-02    500

目前,我得到的是错误的:

company   60 DAYS    30 DAYS   CURRENT   TOTAL
-------   -------    -------   -------   -----
ABC        0           100        0      100
DEF       200           0         0      200 
GHI        0            0        400     400 

应该是:

company   60 DAYS    30 DAYS   CURRENT   TOTAL
-------   -------    -------   -------   -----
ABC        0           100       300     400
DEF       200           0         0      200 
GHI        0            0        900     900 

换句话说,它只显示右列下的1个金额,但不添加它们。 这是我的代码:

<?php       
$now = time();
$result = mysql_query('SELECT *,SUM(amtdue)
FROM quotes AS ql inner join customers as c on ql.customerid = c.cid
WHERE amtdue > 0
GROUP BY company
')or die(mysql_error());

while($row=mysql_fetch_array($result))  
{  
echo "<tr>";
echo '<td>' . $row['company'] . '</td>';

if(floor(($now - strtotime($row['invoicedate']))/(60*60*24))>60) // 60 DAYS
echo '<td align="right">' . $row['SUM(amtdue)'] . '</td>';
else
echo '<td align="right">' . "0.00" . '</td>';

if(floor(($now - strtotime($row['invoicedate']))/(60*60*24))>30 && floor(($now - strtotime($row['invoicedate']))/(60*60*24))<60) // 30 DAYS
echo '<td align="right">' . $row['SUM(amtdue)'] . '</td>';
else
echo '<td align="right">' . "0.00" . '</td>';

if(floor(($now - strtotime($row['invoicedate']))/(60*60*24))<30) // CURRENT
echo '<td align="right">' . $row['SUM(amtdue)'] . '</td>';
else
echo '<td align="right">' . "0.00" . '</td>';

echo '<td align="right">' . $row['SUM(amtdue)'] . '</td>';

echo "</tr>"; 
}
?>

任何帮助将不胜感激!!

2 个答案:

答案 0 :(得分:0)

您可以在查询中获取所需的数据,只需使用php ...检查查询并相应地修改...

select company, 
      sum(case when datediff(now(),invoicedate)>60 then amtdue else 0 end) 60days,
      sum(case when datediff(now(),invoicedate)<=60 and datediff(now(),invoicedate)>=30 then amtdue else 0 end) 30_60days,
      sum(case when datediff(now(),invoicedate)<30 then amtdue else 0 end) Current,
      sum(amtdue) Total
From YourTable
Group by Company;

答案 1 :(得分:0)

如果我的假设是正确的,那么你可以使用

SELECT 
    company, 
    SUM(CASE WHEN TIMESTAMPDIFF(
                      MONTH, 
                      LAST_DAY(invoicedate) + INTERVAL 1 DAY, 
                      LAST_DAY(NOW()) + INTERVAL 1 DAY) > 1 
             THEN amtdue ELSE 0 END) 60days,
    SUM(CASE WHEN TIMESTAMPDIFF(
                      MONTH, 
                      LAST_DAY(invoicedate) + INTERVAL 1 DAY, 
                      LAST_DAY(NOW()) + INTERVAL 1 DAY) = 1 
             THEN amtdue ELSE 0 END) 30days,
    SUM(CASE WHEN TIMESTAMPDIFF(
                      MONTH, 
                      LAST_DAY(invoicedate) + INTERVAL 1 DAY, 
                      LAST_DAY(NOW()) + INTERVAL 1 DAY) = 0 
             THEN amtdue ELSE 0 END) current,
    SUM(amtdue) total
FROM 
    quotes
GROUP BY 
    company;

解释

在LAST_DAY()的帮助下,我们将获得该月的最后一天。我们在双方都增加了一天,所以我们得到了下个月的第一天。这有助于我们在整个月内获得今天和发票之间的差异,其中整月被定义为

  • invoicedate在当月,然后是0
  • invoicedate是在上个月,然后是1
  • invoicedate甚至更旧,然后&gt; 1

然后我们总结这些值,如果它们属于这一类别。

Demo