如何从表记录和分数中获取连续X天数的间隔和值

How to get interval of X number of consecutive days and values from tables records and scores

我正在尝试从两个表中获取 X 个连续日期(间隔)的平均分数。我的意思是日期必须是连续的,基于列 records.status 的值(仅当状态为 T 或 P 时,行值是 selected,尤其是 scores.score)。

例如,如果我 select personid = 133* 的 4 个连续日期的间隔,我喜欢 return 以下(预平均计算,我认为我应该用 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 片段离线了,但这应该是我的示例设置

————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

我用 PHP 函数得到的平均输出

我预计平均值为 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)

更新: 我之前给你的例子帮助我了解了你的需求和你的背景知识(你喜欢开发什么)。

我知道 PHP 解决方案最适合您,但您知道并非所有 MySQL 解决方案都应依靠 PHP。所以,我决定采用我能想到的最好的方法。

我有您在 PHP 上提供的示例,它们足够好,可以更好地了解您正在使用的数据类型。

从这些示例中我看到 records.purdate 和 scores.date 是相同的,您基本上是将 purdate 列复制到 scores.date 列中。这可能是一种冗余,但它将帮助我们获得每个连续日期的开始日期和结束日期。

我需要先提一下,我正在开发 MySQL v5.7,我使用 MySQL Workbench 6.3 作为 IDE(我已经有很长时间没有使用 phpMyAdmin,但是它也应该与它一起工作)。

您需要创建一个存储过程,如果您不知道如何在 phpMyAdmin 中管理它,只需 google 它即可。

我会为您提供一个可用的(已测试):

CREATE PROCEDURE `getConsecutiveInterval`(IN `selectRows` INT, IN `skippedRows` INT)
BEGIN
SET @selectRows = selectRows; 
SET @skippedRows = skippedRows; 

IF skippedRows = 0 THEN
SET @skippedRows = "";
ELSE 
SET @skippedRows = CONCAT(" , " , skippedRows);
END IF;

SET @q = CONCAT("SELECT concat(date_format(MIN(StartDate), '%Y-%m-%d'), '  /  ', date_format(MAX(EndDate), '%Y-%m-%d')) AS Dates, AVG(Score)
FROM (
SELECT 
    a.purdate AS StartDate, 
    b.date AS EndDate, 
    b.score  AS Score
FROM records a 
LEFT JOIN scores b 
ON a.purdate = b.date AND a.personid = b.personid 
WHERE 
    a.personid = 133
AND a.status IN('P','T') 
AND b.score IS NOT NULL
ORDER BY purdate ASC, score DESC 
LIMIT ", @selectRows, @skippedRows, " ", ") D;");

PREPARE ConsecutiveInterval FROM @q;
EXECUTE ConsecutiveInterval;
DEALLOCATE PREPARE ConsecutiveInterval;
END

此存储过程类似于您的 getConsecutiveInterval() 函数,不同之处在于它位于 MySQL.

工作原理: 您可以通过

调用存储过程
CALL getConsecutiveInterval(selectRows,skippedRows)

我在存储过程中设置了一个条件,如果skippedRows为0,那么它将是一个空字符串。否则,skippedRows 将始终被 returned。

例如,使用您提供的示例:

CALL getConsecutiveInterval(4,0)

会 return :

'2015-07-11  /  2016-01-20', '4.25'

CALL getConsecutiveInterval(4,1)

会 return

2016-01-30  /  2016-01-30   4.00

等等。

selectRows var 是您 PHP 中的 $interval,而 skippedRows 是 $offset。

然后,从您的 PHP 那边,您可以通过以下方式调用它:

$query = "CALL getConsecutiveInterval( " . $interval . " , "  . $offset .")";

这样,您将只控制 PHP 上输出的 $interval 和 $offset 整数,其余的将由 MySQL 自己维护。

$offset 计算将与以前相同:

$offset += $interval;

您还可以更改存储过程以使用更多参数(例如 personid、status 等)进行扩展。无论您需要什么参数,您都可以随时扩展它。

例如,我将使用 personid 对其进行扩展:

CREATE PROCEDURE `getConsecutiveInterval`(IN `selectRows` INT, IN `skippedRows` INT, IN personID INT)
BEGIN
SET @selectRows = selectRows; 
SET @skippedRows = skippedRows; 
SET @personid = personID;

IF skippedRows = 0 THEN
SET @skippedRows = "";
ELSE 
SET @skippedRows = CONCAT(" , " , skippedRows);
END IF;

IF personID > 0 THEN 
SET @personid = CONCAT(" AND a.personid = ",  personID); 
ELSE 
SET @personid = ""; 
END IF;

SET @q = CONCAT("SELECT concat(date_format(MIN(StartDate), '%Y-%m-%d'), '  /  ', date_format(MAX(EndDate), '%Y-%m-%d')) AS Dates, AVG(Score)
FROM (
SELECT 
    a.purdate AS StartDate, 
    b.date AS EndDate, 
    b.score  AS Score
FROM records a 
LEFT JOIN scores b 
ON a.purdate = b.date AND a.personid = b.personid 
WHERE 
    a.status IN('P','T') 
AND b.score IS NOT NULL ", @personid, " ORDER BY purdate ASC, score DESC LIMIT ", @selectRows, @skippedRows, " ", ") D;");

PREPARE ConsecutiveInterval FROM @q;
EXECUTE ConsecutiveInterval;
DEALLOCATE PREPARE ConsecutiveInterval;
END

这将添加另一个参数,这样调用:

CALL getConsecutiveInterval(4,0, 133);

133是personid,如果我改成0,那么条件a.personid = 133

将从查询中删除,我将得到一个基于 table 排序的随机数据。

希望这次更新对您的旅程有所帮助。

做了一个测试例子:

declare @selectedIntervalCount int=3, @selectedID int=1
declare @startDate date='2018-01-01',@endDate date='2018-01-31'
declare @data table(pID int,pDate date, statsValue int)
insert into @data(pID,pDate, statsValue)
values(1,'2018-01-01',1)
,(1,'2018-01-02',2),(1,'2018-01-03',3),(1,'2018-01-04',4)
,(1,'2018-01-05',5),(1,'2018-01-06',1),(1,'2018-01-07',2)
,(1,'2018-01-08',7),(1,'2018-01-09',4),(1,'2018-01-10',3)
,(1,'2018-01-11',8),(1,'2018-01-12',5),(1,'2018-01-13',3)


select tt1.tempIX/@selectedIntervalCount 'intervalIX', cast(min(tt1.pDate) as varchar)+' - '+cast(max(tt1.pDate) as varchar) 'interval', sum(tt1.statsValue)/cast(count(tt1.statsValue) as float) 'avgStatsValue' from( 
    select (row_number() over (order by pDate) -1) 'tempIX', t1.pDate, t1.statsValue 
    from @data t1
    where t1.pDate between @startDate and @endDate 
    and t1.pID=@selectedID
) tt1
group by tt1.tempIX/@selectedIntervalCount
order by tt1.tempIX/@selectedIntervalCount

输出为:

intervalIX  interval    avgStatsValue
0   2018-01-01 - 2018-01-03 2
1   2018-01-04 - 2018-01-06 3,33333333333333
2   2018-01-07 - 2018-01-09 4,33333333333333
3   2018-01-10 - 2018-01-12 5,33333333333333
4   2018-01-13 - 2018-01-13 3

如果您的 mysql 版本提供窗口函数,解决方案似乎很简单。

select g.personid, min(g.date) dfrom, max(g.date) dto, avg(g.score) avgscore
  from (
    select s.*, floor((s.rn - 1) / 4) gn
      from (
        select scores.personid, scores.date, scores.score ,
           row_number() over (
             partition by scores.personid
             order by scores.date) as rn
         from scores
         join records
           on  scores.personid = records.personid
           and scores.date = records.purdate
         where records.status in ('T','P')
         order by personid, date
      ) s
  ) g
  group by g.personid, g.gn
  order by g.personid, g.gn;

使用 sql fiddle 中的数据,得出:

+----------+------------+------------+----------+
| personid | dfrom      | dto        | avgscore |
+----------+------------+------------+----------+
|      133 | 2015-07-11 | 2016-01-20 |   4.2500 |
|      133 | 2016-01-30 | 2016-09-28 |   3.0000 |
|      133 | 2016-10-02 | 2017-04-02 |   2.0000 |
|      145 | 2015-06-29 | 2016-06-30 |   3.0000 |
|      145 | 2016-10-24 | 2017-01-16 |   3.3333 |
|      156 | 2015-10-20 | 2015-12-17 |   2.0000 |
|      156 | 2015-12-19 | 2016-05-21 |   3.0000 |
|      156 | 2016-05-25 | 2016-10-16 |   4.7500 |
|      156 | 2017-01-30 | 2017-01-30 |   4.0000 |
+----------+------------+------------+----------+