根据时间和日期平均数字

时间:2015-03-13 21:29:26

标签: mysql union average

所以我选择每15分钟将数据添加到数据库中的最新x小时范围。我想通过平均过去那天当时添加的所有数据,在这个数据预测中添加下一个x小时的计数。

以下是我正在使用的两个表的示例。我无法在计数表中提供足够的数据来表示我想要做的事情。

 NAME Table
+---------+-------+
| NAME_ID | NAME  |
+---------+-------+
| 1       | name1 |
+---------+-------+

COUNT Table
+----+---------+-------+---------------------+
| ID | NAME_ID | COUNT | DTM                 |
+----+---------+-------+---------------------+ 
| 1  | 1       | 4     | 2015-03-08 23:45:00 |
+----+---------+-------+---------------------+
| 2  | 1       | 5     | 2015-03-09 00:00:00 |
+----+---------+-------+---------------------+
| 3  | 1       | 6     | 2015-03-09 00:15:00 |
+----+---------+-------+---------------------+
| 4  | 1       | 2     | 2015-03-09 00:30:00 |
+----+---------+-------+---------------------+
| 5  | 1       | 5     | 2015-03-09 00:45:00 |
+----+---------+-------+---------------------+
| 6  | 1       | 6     | 2015-03-09 01:00:00 |
+----+---------+-------+---------------------+
| 7  | 1       | 6     | 2015-03-09 01:15:00 |
+----+---------+-------+---------------------+
| 8  | 1       | 3     | 2015-03-09 01:30:00 |
+----+---------+-------+---------------------+

以下是我想要的结果示例。

+----+-------+-------+---------------------------------------------------------------------------+
| ID | NAME  | COUNT | DTM                                                                       |
+----+-------+-------+---------------------------------------------------------------------------+
| 1  | name1 | 5     | 2015-03-09 00:45:00 <-----Past Data                                       |
+----+-------+-------+---------------------------------------------------------------------------+
| 2  | name1 | 6     | 2015-03-09 01:00:00 <-----Past Data                                       |
+----+-------+-------+---------------------------------------------------------------------------+
| 3  | name1 | 6     | 2015-03-09 01:15:00 <-----Past Data                                       |
+----+-------+-------+---------------------------------------------------------------------------+
| 4  | name1 | 3     | 2015-03-09 01:30:00 <-----CURRENT TIME                                    |
+----+-------+-------+---------------------------------------------------------------------------+
| 5  | name1 | 4     | 2015-03-09 01:45:00 <-----Average count for all counts with matching day of the week and time |
+----+-------+-------+---------------------------------------------------------------------------+
| 6  | name1 | 5     | 2015-03-09 02:00:00 <-----Average count for all counts with matching day of the week and time|
+----+-------+-------+---------------------------------------------------------------------------+
| 7  | name1 | 6     | 2015-03-09 02:15:00 <-----Average count for all counts with matching day of the week and time|
+----+-------+-------+---------------------------------------------------------------------------+
| 8  | name1 | 2     | 2015-03-09 02:30:00 <-----Average count for all counts with matching day of the week and time|
+----+-------+-------+---------------------------------------------------------------------------+

这是我目前的尝试,但没有得到我需要的结果。

SELECT * FROM (
  select c.count, c.dtm 
  from count c 
  join name n on c.name_id = n.name_id
  where NAME Like @name
  union
  SELECT AVG(c.count), c.dtm
  from count c  
  join name n on c.name_id = n.name_id
  where (n.NAME Like @name)
  AND (c.DTM < NOW())
  LIMIT 20
) a
ORDER BY DTM DESC

0 个答案:

没有答案