开发者

Fetch views by (unique viewers and unique videos)

This is little bit complicated SQL query wich needs to fetch all the views by uni开发者_高级运维que viewers and unique videos, Because multiple views of the same video by the same user can be recorded in the SQL previously.

So:

Views(user,video)
--------------
joker  |  1541
foo    |  1541
foo    |  1541
bar    |  1542
bar    |  1543

The should fetch:

joker  |  1541
foo    |  1541
bar    |  1542
bar    |  1543

Facts:

  • It's OK that the same person can appear but viewed different videos
  • It's OK for the same video to appear but viewed by different persons
  • It's NOT ok for the same person viewed the same video to appear multiple times.

It maybe a little bit hard to get in the first time, and may sound useless but it's helpful for my case. but appreciate any help.

Thanks!


select user, video
    from Views
    group by user, video


SELECT DISTINCT user,video FROM Views

Here's an example I did on Oracle to prove this works:

DROP TABLE views;
CREATE TABLE views (USR VARCHAR2(100), video NUMBER);

INSERT INTO views VALUES ('joker', 1541);
INSERT INTO views VALUES ('foo', 1541);
INSERT INTO views VALUES ('foo', 1541);
INSERT INTO views VALUES ('bar', 1542);
INSERT INTO views VALUES ('bar', 1543);
COMMIT;

SELECT DISTINCT usr,video FROM views
ORDER BY usr,video;

Results:

    USR     VIDEO
1   bar     1542
2   bar     1543
3   foo     1541
4   joker   1541
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜