0

好吧,让我重新开始。

首先我们有三个表:

表:交易

 meter_id | bay_number |      trans_date_time       | amount
----------+------------+----------------------------+--------
     1078 |          5 | 2013-06-03 09:59:32+10     |   5.00
     1078 |          7 | 2013-06-03 09:12:01+10     |   5.00
     1078 |          6 | 2013-06-03 08:33:11+10     |   5.00
     1078 |          8 | 2013-05-27 09:48:26+10     |   5.00
     1078 |          5 | 2013-05-27 09:41:42+10     |   5.00
     1078 |          4 | 2013-05-27 09:41:08+10     |   5.00
     1078 |          7 | 2013-05-20 17:14:15+10     |   2.00
     1078 |          2 | 2013-05-20 16:19:10+10     |   2.50

表:服务

 meter_id |      notified_at       |      finished_at
----------+------------------------+------------------------
     1078 | 2013-05-30 15:02:27+10 | 2013-05-30 15:32:20+10
     1078 | 2013-05-30 12:32:43+10 | 2013-05-30 14:50:14+10
     1078 | 2013-05-30 08:31:27+10 | 2013-05-30 10:25:56+10
     1078 | 2013-05-29 07:36:31+10 | 2013-05-29 11:35:47+10

表:relocated_meter

 relocation_date | meter_id | bay_number | source_meter_id | source_bay_number
-----------------+----------+------------+-----------------+-------------------
 2013-04-24      |     1078 |          1 |            1078 |                 1
 2013-04-24      |     1078 |          2 |            1078 |                 2
 2013-04-24      |     1078 |          3 |            1078 |                 3
 2013-04-24      |     1078 |          4 |            1078 |                 4
 2013-04-24      |     1078 |          5 |            1078 |                 5
 2013-04-24      |     1078 |          6 |            1078 |                 6
 2013-04-24      |     1078 |          7 |            1078 |                 7
 2013-04-24      |     1078 |          8 |            1067 |                 5
 2013-04-24      |     1078 |          9 |            1067 |                 6

我正在尝试编写的是 PHP 类中的一些优雅函数以生成以下内容:

对于每条服务记录,每台电表的收入是多少?然后,产生过去 52 周的收入(例如,如果电表 1078 从本周二 10:00 到 10:30 下降,那么同一电表在上周二 10:00 到 10:30 之间的收入是多少,以及前一周一直到 52 周)。这样我就可以在同一时间段内获得每米的年平均值。

现在,几米已经继承了一些海湾,并且引入了第三张名为 relocated_meter 的表,我想获得另一个米(1067 海湾 5 和 1067 海湾 6)的每周金额加上收入,因为它们现在是米 1078 海湾 8 和 9,因此平均收入也应包括其他海湾对于 relocated_date 之前的日期

我希望这是有道理的?


function getWeeks()
{

    $meter_combo        = array(
                                array(1234 => 1),
                                array(1234 => 2),
                                array(1234 => 3),
                                array(1234 => 4),
                            );

    $notified_time   = '2013-05-17 12:20:48';
    $completed_time  = '2013-05-17 12:52:07';
    $relocation_date = '2013-04-30 00:00';

    $old_meter_combo    = array(
                                array(4444 => 1),
                                array(4444 => 2),
                                array(4444 => 8),
                                array(4444 => 9),
                            );

    $notified_time   = strtotime($notified_time);
    $completed_time  = strtotime($completed_time);
    $relocation_date = date($relocation_date);

    $weeks = 2;

    $combined = array();    
    for($i=0;$i<=$weeks;$i++)
    {       
        $start_series = strtotime("- $i weeks", $notified_time);
        $end_series   = strtotime("- $i weeks", $completed_time);


        if(strtotime($relocation_date) < $start_series) {
            $combined[] = array(
                            'week_id'      => $i,
                            'start_time'   => date('Y-m-d H:i:s',$start_series),
                            'end_time'     => date('Y-m-d H:i:s',$end_series),
                            'relocation'   => $relocation_date,
                            'meter_id'     => $meter_combo,
                            );
        } else {
            $combined[] =  array(
                             'week_id'      => $i,
                             'start_time'   => date('Y-m-d H:i:s',$start_series),
                             'end_time'     => date('Y-m-d H:i:s',$end_series),
                             'relocation'   => $relocation_date,
                             'meter_id'     => $old_meter_combo,
                             );
        }
    }


    $sql1 = "SUM(CASE WHEN (";
    foreach($combined as $k1 => $v1)
    {

        $sql2 = "AND trans_date_time BETWEEN " . $v1['start_time'] . " AND " .$v1['end_time']. "<br/>" ;
        $sql3 = "THEN amount ELSE 0 END) AS week_" . $v1['week_id'] . "<br />" ;

        foreach ($v1['meter_id'] as $v2) {
            foreach ($v2 as $meter => $bay) {
                $sql4[] = "meter_id = $meter and bay_number = $bay <br/>";
            }
        }
    }

    $sql4 = implode(" OR ", $sql4);

    echo $sql1.$sql4.$sql2.$sql3;


}

任何其他优雅的这样做都非常受欢迎:)

谢谢。

4

2 回答 2

1

我对你的代码做了一个小改动,没什么特别的。

$sql1 = "SUM(CASE WHEN (";
foreach($combined as $k1 => $v1)
{

    $sql2 = "AND trans_date_time BETWEEN " . $v1['start_time'] . " AND " .$v1['end_time']. " " ;
    $sql3 = "THEN amount ELSE 0 END) AS week_" . $v1['week_id'] . " " ;

    foreach($v1 as $k2)
    {
        foreach($v1['meter_id'] as $k3 => $v3)
        {
            foreach($v3 as $meter => $bay)
            {
                $sql4[] = "meter_id = $meter and bay_number = $bay ";
            }


        }

    }

}

$sql4 = implode(" OR ", $sql4);


echo $sql1 . $sql4 . $sql2 . $sql3;
于 2013-07-08T06:49:45.700 回答
1

我对你的代码做了一个小改动

$sql1 = "SUM(CASE WHEN (";
foreach($combined as $k1 => $v1)
{

    $sql2 = "AND trans_date_time BETWEEN " . $v1['start_time'] . " AND " .$v1['end_time']. "<br/>" ;
    $sql3 = "THEN amount ELSE 0 END) AS week_" . $v1['week_id'] . "<br />" ;

    foreach ($v1['meter_id'] as $v2) {
        foreach ($v2 as $meter => $bay) {
            $sql4[] = "meter_id = $meter and bay_number = $bay <br/>";
        }
    }
}

$sql4 = implode(" OR ", $sql4);

echo $sql1.$sql4.$sql2.$sql3;
于 2013-07-08T07:28:27.200 回答