1

我们的仪表板上显示了前十名视频列表。有时我们会遇到这样一种情况,一位用户发布了 6 个视频,并且所有视频都进入了前十名(观看次数最多)。我的老板只想显示前十名中任何一名成员的前两名。

如何做到这一点。这是我用来提取数据的脚本:

SELECT a.MediaID,
COUNT(a.MediaID) AS TimesViewed
FROM MediaViewLog a
INNER JOIN MemberVideo b ON b.MemberVideoID = a.MediaID
INNER JOIN Member c ON c.MemberID = b.MemberID
WHERE a.ViewDate BETWEEN '5/25/2013 10:04:23 AM' AND '12/12/2050 11:59:59 PM'
AND a.MediaType = 'V'
AND b.CreateDate > '5/25/2013 10:04:23 AM'
AND c.SBIcon = 'N'
GROUP BY a.MediaID

MediaViewLog 包含以下数据:

+----------------+---------+-----------+-------------------------+
| MediaViewLogID | MediaID | MediaType | ViewDate                |
+----------------+---------+-----------+-------------------------+
| 3336           | 7033    | V         | 2013-05-26 03:36:52.573 |
+----------------+---------+-----------+-------------------------+
| 3337           | 7037    | V         | 2013-05-26 04:22:16.682 |
+----------------+---------+-----------+-------------------------+
| 3338           | 12356   | A         | 2013-05-26 03:36:52.573 |
+----------------+---------+-----------+-------------------------+
| 3339           | 7102    | V         | 2013-05-26 07:12:25.428 |
+----------------+---------+-----------+-------------------------+
| 3340           | 7058    | V         | 2013-05-26 08:02:27.003 |
+----------------+---------+-----------+-------------------------+
| 3341           | 7033    | B         | 2013-05-26 03:36:52.573 |
+----------------+---------+-----------+-------------------------+

在“会员”表中,每个媒体都与一个会员相关联。“MemberID”是主键

任何想法,如果任何成员不止一个,我如何才能提取前 2 个,作为前 10 名的一部分。

任何帮助将不胜感激。

谢谢!

4

1 回答 1

4

为此,请使用以下row_number()功能:

select t.MediaId, t.TimesViewed
from (SELECT a.MediaID, c.MemberId, COUNT(a.MediaID) AS TimesViewed,
             row_number() over (partition by c.MemberId order by count(a.MediaId) desc
                               ) as seqnum
      FROM MediaViewLog a
      INNER JOIN MemberVideo b ON b.MemberVideoID = a.MediaID
      INNER JOIN Member c ON c.MemberID = b.MemberID
      WHERE a.ViewDate BETWEEN '5/25/2013 10:04:23 AM' AND '12/12/2050 11:59:59 PM'
      AND a.MediaType = 'V'
      AND b.CreateDate > '5/25/2013 10:04:23 AM'
      AND c.SBIcon = 'N'
      GROUP BY a.MediaID, c.MemberId
     ) t
where seqnum <= 2;

这是假设每个视频仅由一个成员发布。也就是说,按MediaId和按分组a.MediaId, c.MemberId实际上是一回事。否则,您将混合来自不同用户的计数(在视频的“发布”端,而不是观看),并且为每个用户仅提取两个要困难得多。

我也认为那里应该有一个top 10

于 2013-05-27T15:18:25.440 回答