Musterknabe
Musterknabe

Reputation: 6101

Get database records which are within a specific timeframe

Let's assume I have the following table called video_data. I have another one videos which isn't really relevant though. I use it to query videos for a specific channel, but that wouldn't change the query. It's just to get the calculations for one channel instead of all.

+----+----------+-------+---------------------+
| id | video_id | views |     created_at      |
+----+----------+-------+---------------------+
|  1 |        1 |  1000 | 2016-04-26 00:00:00 |
|  2 |        2 |   500 | 2016-04-26 00:00:01 |
|  3 |        3 |  2500 | 2016-04-26 00:00:02 |

|  4 |        1 |  1500 | 2016-04-26 02:00:00 |
|  5 |        2 |  1000 | 2016-04-26 02:00:01 |
|  6 |        3 |  3000 | 2016-04-26 02:00:02 |

|  7 |        1 |  5000 | 2016-04-26 04:00:00 |
|  8 |        2 | 10000 | 2016-04-26 04:00:01 |
|  9 |        3 | 30000 | 2016-04-26 04:00:02 |
+----+----------+-------+---------------------+

What I want to do now is to get the average value of views inside a time frame. Let's say, I want to get the average views a video has within 2 hours. Let's do this with the video with the video_ 1 as an example

So what I would need to do would be the following. I would need to get the average value of id: 1 and id: 4. This would be 1250 because it's (1000 + 1500) / 2. Next I would need to get the average of id: 4 and id: 7. This would be 3250 because it's (1500 + 5000) / 2. Now the average the video gets in two hours would be 2250 because it's (1250 + 3250) / 2, correct?

Now what I have no idea, is how to get this from MySQL. Is it even possible to do in plain MySQL? I need this for many, many video_data. Like I have data for over 100 hours! And if the data with id: 100 and id: 105 are less than two hours apart from each other I would still need this data to be also calculated for the calculation.

I wanted to do it somehow like this

select *
from `video_data`
where `video_id` in (select `id` from `videos` where `channel_id` = 1)
  and TIMEDIFF(`created_at`, `created_at`) < '02:00:00'

but this simply returns me every result because the result of TIMEDIFF is always 00:00:00

I created an SQL Fiddle for this

MySQL 5.6 Schema Setup:

CREATE TABLE `video_data` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `video_id` int(10) unsigned NOT NULL,
  `shares` int(11) DEFAULT NULL,
  `likes` int(11) DEFAULT NULL,
  `comments` int(11) DEFAULT NULL,
  `total_count` int(11) DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

INSERT INTO `video_data` (`id`, `video_id`, `shares`, `likes`, `comments`, `total_count`, `created_at`, `updated_at`)
VALUES
    (1889612, 245721, 777, 69922, 1314, 72013, '2015-10-04 20:00:04', '2015-10-04 20:00:04'),
    (1896986, 245721, 970, 90611, 1570, 93151, '2015-10-04 21:00:04', '2015-10-04 21:00:04'),
    (1904145, 245721, 1121, 104636, 1725, 107482, '2015-10-04 22:00:05', '2015-10-04 22:00:05'),
    (1911872, 245721, 1199, 115389, 1838, 118426, '2015-10-04 23:00:04', '2015-10-04 23:00:04'),
    (1882621, 245007, 1651, 102569, 2659, 106879, '2015-10-04 19:00:06', '2015-10-04 19:00:06'),
    (1889613, 245007, 1769, 113910, 2775, 118454, '2015-10-04 20:00:05', '2015-10-04 20:00:05'),
    (1896988, 245007, 1829, 121646, 2851, 126326, '2015-10-04 21:00:05', '2015-10-04 21:00:05'),
    (1904150, 245007, 1889, 127677, 2917, 132483, '2015-10-04 22:00:06', '2015-10-04 22:00:06'),
    (1911877, 245007, 1914, 132764, 2957, 137635, '2015-10-04 23:00:05', '2015-10-04 23:00:05'),
    (1845984, 239950, 675, 75030, 1373, 77078, '2015-10-04 12:00:04', '2015-10-04 12:00:04'),
    (1849749, 239950, 857, 97028, 1617, 99502, '2015-10-04 13:00:05', '2015-10-04 13:00:05'),
    (1853996, 239950, 1021, 113648, 1801, 116470, '2015-10-04 14:00:04', '2015-10-04 14:00:04'),
    (1858726, 239950, 1148, 126624, 1919, 129691, '2015-10-04 15:00:04', '2015-10-04 15:00:04'),
    (1863954, 239950, 1297, 137950, 2019, 141266, '2015-10-04 16:00:04', '2015-10-04 16:00:04'),
    (1869723, 239950, 1427, 148069, 2102, 151598, '2015-10-04 17:00:04', '2015-10-04 17:00:04'),
    (1875982, 239950, 1549, 156391, 2194, 160134, '2015-10-04 18:00:05', '2015-10-04 18:00:05'),
    (1882622, 239950, 1618, 161312, 2232, 165162, '2015-10-04 19:00:07', '2015-10-04 19:00:07'),
    (1889616, 239950, 1683, 164783, 2261, 168727, '2015-10-04 20:00:06', '2015-10-04 20:00:06'),
    (1896990, 239950, 1722, 167718, 2278, 171718, '2015-10-04 21:00:06', '2015-10-04 21:00:06'),
    (1904151, 239950, 1743, 170240, 2290, 174273, '2015-10-04 22:00:07', '2015-10-04 22:00:07'),
    (1911880, 239950, 1761, 172363, 2300, 176424, '2015-10-04 23:00:06', '2015-10-04 23:00:06');

When I now execute the query

select avg(pd.shares) AS shares, avg(pd.likes) AS likes, avg(pd.comments) AS comments FROM video_data pd JOIN video_data pd1 ON pd1.video_id = pd.`video_id` AND TIMEDIFF(pd.created_at, pd1.created_at) <= '02:00:00';

+-----------+-------------+-----------+
|  shares   |    likes    | comments  |
+-----------+-------------+-----------+
| 1298.2077 | 123542.5769 | 2032.2769 |
+-----------+-------------+-----------+

But when going over the results it looks like the likes value is the average of ALL likes in the database, and not only the ones who are only 2 hours apart from each other, right? Or is it correct?

Upvotes: 3

Views: 105

Answers (2)

Andrews B Anthony
Andrews B Anthony

Reputation: 1381

select t.*,avg(t1.views) from videos t join videos t1 on
t1.video_id=t.video_id 
and timediff(t.created_at,t1.created_at)< '02:00:00' 
group by t.video_id

try this query it should work

Upvotes: 1

Marcin Orlowski
Marcin Orlowski

Reputation: 75645

but this simply returns me every result because the result of TIMEDIFF is always 00:00:00

It does so becauase you used the same column:

TIMEDIFF(`created_at`, `created_at`)

so it's hardly possible to make it produce different result. You perhaps wanted to use NOW() as one of the arguments?

Upvotes: 0

Related Questions