[更新逻辑更新]
希望通过添加表格,sqlfidddle和我现在使用的php函数,我的问题更加清晰。
这是我正在使用的表的简单表示:
abc
这是一个简单的方法: http://sqlfiddle.com/#!9/b4456e/3
A /现在我需要x天的间隔记录,其中Status = P或L.所以我需要一个计数器停在哪里找到30x代码P或L(跳过T,U,A和空日期)
我需要考虑日期范围内的所有日期也是空的一次。这还包括没有输入(=无代码)的日期,对于这些日期,我可以将代码设置为NULL(它们主要代表周末和假日)。
目前我正在测试以下php函数(tx astrangeloop),它只会使间隔块没有状态逻辑(= P或L)
app/abc.js
使用以下辅助函数来获取以天为单位的开始 - 结束间隔之间的范围:
Operating System: Windows 7
Apache Tomcat: version 7, running on PORT 8090.
Java version: 8
为了从表中获取记录,我使用了这个简单的函数
+-------------+--------+------------+----------+
| person | status | purdate | personid |
+-------------+--------+------------+----------+
| person_name | A | 2016-06-03 | 133 |
| person_name | A | 2016-06-06 | 133 |
| person_name | L | 2016-06-07 | 133 |
| person_name | U | 2016-06-12 | 133 |
| person_name | L | 2016-06-22 | 133 |
| person_name | A | 2016-06-22 | 133 |
| person_name | L | 2016-06-24 | 133 |
| person_name | A | 2016-07-01 | 133 |
| person_name | T | 2016-07-02 | 133 |
| person_name | U | 2016-07-03 | 133 |
| person_name | T | 2016-07-11 | 133 |
| person_name | A | 2016-08-05 | 133 |
| person_name | P | 2016-08-10 | 133 |
| person_name | T | 2016-08-12 | 133 |
| person_name | L | 2016-08-13 | 133 |
| person_name | T | 2016-08-18 | 133 |
| person_name | P | 2016-08-19 | 133 |
| person_name | U | 2016-08-20 | 133 |
| person_name | U | 2016-08-25 | 133 |
| person_name | A | 2016-08-26 | 133 |
| person_name | T | 2016-09-12 | 133 |
| person_name | U | 2016-09-14 | 133 |
| person_name | U | 2016-09-17 | 133 |
| person_name | U | 2016-09-27 | 133 |
| person_name | P | 2016-09-30 | 133 |
| person_name | L | 2016-10-09 | 133 |
| person_name | A | 2016-10-17 | 133 |
| person_name | L | 2016-10-23 | 133 |
| person_name | U | 2016-10-26 | 133 |
| person_name | L | 2016-10-27 | 133 |
| person_name | P | 2016-10-29 | 133 |
| person_name | T | 2016-11-01 | 133 |
| person_name | U | 2016-11-03 | 133 |
| person_name | P | 2016-11-05 | 133 |
| person_name | U | 2016-11-06 | 133 |
| person_name | T | 2016-11-10 | 133 |
| person_name | U | 2016-11-16 | 133 |
| person_name | U | 2016-11-22 | 133 |
| person_name | L | 2016-11-22 | 133 |
| person_name | P | 2016-11-29 | 133 |
| person_name | A | 2016-12-03 | 133 |
| person_name | P | 2016-12-08 | 133 |
| person_name | A | 2016-12-09 | 133 |
| person_name | A | 2016-12-10 | 133 |
| person_name | A | 2016-12-14 | 133 |
| person_name | T | 2016-12-16 | 133 |
| person_name | U | 2016-12-16 | 133 |
| person_name | T | 2016-12-17 | 133 |
| person_name | L | 2016-12-23 | 133 |
| person_name | P | 2016-12-23 | 133 |
| person_name | T | 2016-12-23 | 133 |
| person_name | A | 2016-12-24 | 133 |
| person_name | T | 2016-12-26 | 133 |
| person_name | P | 2016-12-27 | 133 |
| person_name | A | 2016-12-30 | 133 |
| person_name | A | 2016-12-31 | 133 |
| person_name | A | 2017-01-01 | 133 |
| person_name | A | 2017-01-01 | 133 |
| person_name | P | 2017-01-03 | 133 |
| person_name | A | 2017-01-08 | 133 |
| person_name | L | 2017-01-09 | 133 |
| person_name | T | 2017-01-10 | 133 |
| person_name | T | 2017-01-12 | 133 |
| person_name | L | 2017-01-16 | 133 |
| person_name | U | 2017-01-21 | 133 |
| person_name | U | 2017-01-21 | 133 |
| person_name | L | 2017-01-22 | 133 |
| person_name | P | 2017-01-25 | 133 |
| person_name | T | 2017-01-31 | 133 |
| person_name | T | 2017-02-02 | 133 |
| person_name | P | 2017-02-05 | 133 |
| person_name | T | 2017-02-07 | 133 |
| person_name | A | 2017-02-10 | 133 |
| person_name | P | 2017-02-18 | 133 |
| person_name | L | 2017-02-18 | 133 |
| person_name | U | 2017-02-19 | 133 |
| person_name | A | 2017-02-26 | 133 |
| person_name | P | 2017-03-08 | 133 |
| person_name | L | 2017-03-17 | 133 |
| person_name | U | 2017-03-18 | 133 |
| person_name | L | 2017-03-18 | 133 |
| person_name | A | 2017-03-23 | 133 |
| person_name | U | 2017-03-23 | 133 |
| person_name | U | 2017-03-24 | 133 |
| person_name | A | 2017-03-27 | 133 |
| person_name | P | 2017-03-28 | 133 |
| person_name | L | 2017-03-30 | 133 |
| person_name | U | 2017-03-31 | 133 |
| person_name | A | 2017-04-05 | 133 |
| person_name | P | 2017-04-07 | 133 |
| person_name | P | 2017-04-10 | 133 |
| person_name | T | 2017-04-10 | 133 |
| person_name | A | 2017-04-11 | 133 |
| person_name | A | 2017-04-19 | 133 |
| person_name | A | 2017-04-22 | 133 |
| person_name | P | 2017-04-24 | 133 |
| person_name | U | 2017-04-26 | 133 |
| person_name | U | 2017-04-28 | 133 |
| person_name | U | 2017-05-06 | 133 |
| person_name | L | 2017-05-08 | 133 |
+-------------+--------+------------+----------+
最后,我想计算每个区间的平均值,如总和(P和L的实例)/总和(' P',' L'' T& #39;,' U' A' A')在此时间间隔内。
我需要总和(P和L的实例)进行进一步的计算
B / [编辑]会问一个新问题,将A与在tableB中查找incidentDate结合起来
[编辑] 我正在测试这个逻辑
function getRecordsWithIntervalsFault(array $records, $interval_size)
{
$intervals = array();
foreach (array_chunk($records, $interval_size) as $interval_records) {
$first_record = reset($interval_records);
$last_record = end($interval_records);
$total = count($records);
$interval = array(
'start' => $first_record['purdate'],
'end' => $last_record['purdate'],
'codes' => array_column($interval_records, 'status'),
'count' => count($interval_records),
'periodLength' => dateRangeInDays($first_record['purdate'], $last_record['purdate']),
'total' => $total,
);
$intervals[] = $interval;
}
return $intervals;
}
$recordsForJs = getRecordsWithIntervalsFault($records, 30);
echo '<pre>';
print_r($recordsForJs);
echo '</pre>';