【发布时间】:2018-02-03 19:45:50
【问题描述】:
我正在尝试从两个表中获取 X 个连续日期(间隔)的平均分数。有了这个,我的意思是日期必须根据列 records.status 的值是连续的(仅当状态为 T 或 P 时,才会选择行值,尤其是 score.score)。
例如,如果我为 personid = 133* 选择 4 个连续日期的间隔,我希望返回以下内容(预平均计算,我认为我应该使用 SQL 查询获得?)
2015-07-11 5
2015-10-17 2
2015-11-06 5
2016-01-20 5
2016-01-30 4
2016-05-19 4
2016–09-07 1
2016-09-28 3
2016-12-29 2
2017-01-17 1
2017-01-22 3
2017-04-02 2
并绘制图表(经过平均计算,我认为我需要用 PHP 来做)
group 1 (2015-07-11 / 2016-01-20) 4.25
group 2 (2016-01-30 / 2016-09-28) 3.00
group 3 (2016-12-29 / 2017-04-02) 2.00
*这是我随机生成的一些样本数据,我正在测试,但我的实际数据更大,结构更好,包含更多列和真正连续的日期(周一至周五,隔天)。
http://sqlfiddle.com/#!9/4b7a62/1
非常欢迎任何提示和建议。
MySQL 版本:5.6.26 [edit1] 不知何故,我的 sqlfiddle sn-p 离线,但这应该是我的示例设置
————2 DB tables schema’s
CREATE TABLE IF NOT EXISTS `records` (
`person` varchar(32) NOT NULL,
`status` varchar(32) NOT NULL,
`purdate` date NOT NULL,
`personid` int(11) DEFAULT NULL,
`id` int(11) NOT NULL
)
CREATE TABLE IF NOT EXISTS `scores` (
`personid` int(11) DEFAULT NULL,
`score` int(11) DEFAULT NULL,
`date` date DEFAULT NULL,
`id` int(11) NOT NULL
)
—-php for sample data—
function getRandomDateTime($startDate, $endDate, $num) {
for ($i = 0; $i < $num; $i++) {
$dateArr[] = date('Y-m-d', mt_rand(strtotime($startDate), strtotime($endDate)));
}
sort($dateArr, SORTDATE);// SORT_REGULAR);SORTDATE);//
return $dateArr;
}
$test = getRandomDateTime('2015-06-03', '2017-05-12', 100);
echo "insert into records (person, status, purdate, personID) values\r\n";
foreach($test as $value) {
$arrCode = ['P','L','T'];
$arrId = [133, 145,156];
$rand = $arrCode[array_rand($arrCode, 1)];
$randID = $arrId[array_rand($arrId, 1)];
echo "('person_name', '".$rand."', '".$value."', '".$randID."'),\r\n";
}
echo "insert into scores (personID, score) values\r\n";
for ($i=0;$i < 100;$i++) {
$arrId = [133, 145,156];
$randID = $arrId[array_rand($arrId, 1)];
echo "('".$randID."','".rand(1,5)."'),\r\n";
}
——— SQL Query To Update The Date Column—
UPDATE scores
SET scores.date = (
SELECT records.purdate
FROM records
WHERE records.id = scores.id
);
[edit2] 这个简单的 php 函数,我称之为:。 getConsecutiveInterval(4)。
function getConsecutiveInterval($interval) {
global $conn;
// $interval = 4;
$offset = '';
// For loop will control the results sets divided by 4
for ($i = 1; $i <= $interval; $i++) {
// To add the offset after the first set
if ($offset > 0) {
$limitValues = $interval . ", " . $offset . " ";
} else {
$limitValues = $interval;
}
// Query is the same and at the end of it you include LIMIT to be controlled by the loop.
$q = "SELECT a.purdate, b.score, a.status "
. "FROM records a "
. "INNER JOIN scores2 b "
. "ON a.purdate = b.date AND a.personid = b.personid "
. "WHERE a.personid = 133 AND a.status IN('P','T') "
. "ORDER BY purdate ASC, score DESC ";
$sqlquery = $q . " LIMIT " . $limitValues;
$avg = 0;
$total = 0;
//Total Found Use To Divide by ... For Max Loop
$result = mysqli_query($conn, $q);
$num_rows = mysqli_num_rows($result);
//end
foreach (mysqli_query($conn, $sqlquery) as $results) {
// Do Something
$total += $results['score'];
$avg = $total / $interval;
}
echo $avg . '<br/>';
$offset += $interval;
} echo '<hr/>';
}
我知道随机数据输出不同的平均值,但基于我的以下随机数据和硬编码的 personid = 133
我预计平均值为2.75, 3.5 and 3.5(based on the rest 2 dates not 4)
当我使用 getConsecutiveInterval(3);我希望平均值为3.33, 3.33, 2.66 and 4 (based on 1 date)
【问题讨论】:
-
您需要将样本数据放入问题中,最好是表格。您在问题中的示例有一个日期,但您的数据有两个。
-
什么版本的 MySQL 或 MariaDB?
-
@MattW。 SQL Fiddle 有问题?
-
啊我没看到链接
-
@GordonLinoff 我已经制作了一些示例数据,但是两个表都有一个相等的列日期和一个相等的人 ID 是正确的 GerardH.Pille 我正在使用 mysql 5.6.26(我编辑了我的问题)