Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7893187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:04:04+00:00 2026-06-03T07:04:04+00:00

I have an Intranet site that only registered users can watch videos. Users can

  • 0

I have an Intranet site that only registered users can watch videos. Users can watch videos on several platforms like iPhone, iPad, Android or Web and I track all those information (for the simplicity of the problem, please do not consider other platforms). I am trying to display some video watch reports at admin panel.

Table structure is below (I have lots of other columns but they are not important for this problem);

CREATE TABLE IF NOT EXISTS `history` (
  `history_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `video_id` int(11) unsigned NOT NULL,
  `user_id` int(11) unsigned NOT NULL,
  `platform_id` int(11) unsigned NOT NULL,
  `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`history_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=9 ;

INSERT INTO `history` (`history_id`, `video_id`, `user_id`, `platform_id`, `created`) VALUES
(1, 1, 1, 1, '2012-05-06 08:13:57'),
(2, 2, 1, 1, '2012-05-06 13:23:57'),
(3, 1, 1, 4, '2012-05-06 18:16:39'),
(4, 4, 2, 3, '2012-05-07 08:14:19'),
(5, 1, 2, 3, '2012-05-07 08:14:55'),
(6, 2, 1, 1, '2012-05-07 15:14:55'),
(7, 3, 2, 1, '2012-05-07 18:05:14'),
(8, 3, 1, 4, '2012-05-07 18:15:24');

CREATE TABLE IF NOT EXISTS `sys_list_of_values` (
  `value_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `display_text` text COLLATE utf8_unicode_ci NOT NULL,
  `list_value` text COLLATE utf8_unicode_ci NOT NULL,
  `list_group` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`value_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=10 ;

INSERT INTO `sys_list_of_values` (`value_id`, `display_text`, `list_value`, `list_group`) VALUES
(1, 'iPhone', 'iphone', 'platform'),
(2, 'Android', 'android', 'platform'),
(3, 'iPad', 'ipad', 'platform'),
(4, 'Website', 'web', 'platform'),
(5, 'Active', 'active', 'status'),
(6, 'Passive', 'passive', 'status'),
(7, 'Waiting for approvement', 'waiting', 'status'),
(8, 'Spam', 'spam', 'status'),
(9, 'Deleted', 'deleted', 'status');

CREATE TABLE IF NOT EXISTS `users` (
  `user_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `username` varchar(80) COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`user_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=3 ;

INSERT INTO `users` (`user_id`, `username`) VALUES
(1, 'test_user_1'),
(2, 'test_user_2');

CREATE TABLE IF NOT EXISTS `videos` (
  `video_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `user_id` int(11) unsigned NOT NULL,
  `title` varchar(200) COLLATE utf8_unicode_ci NOT NULL,
  `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `status` int(11) unsigned NOT NULL,
  PRIMARY KEY (`video_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=5 ;

INSERT INTO `videos` (`video_id`, `user_id`, `title`, `created`, `status`) VALUES
(1, 1, 'Test Video 1', '2012-05-07 08:12:52', 7),
(2, 1, 'Test Video 2', '2012-05-07 08:12:52', 5),
(3, 1, 'Test Video 3', '2012-05-07 08:13:17', 5),
(4, 1, 'Test Video 4', '2012-05-07 08:13:17', 6);

I am trying to display a report like below;

Platform      |     Watch_Count
===============================
iPhone                  20
iPad                     0
Android                  2
Website                120
Total                  142

I have some filter options, like video_id, created, platform etc. For example; I want to display a total watch report for all videos, or for a specific video (video_id), or between dates (created) etc. I also want to display all platforms, whether they are 0 or anything.

The following query displays only iPhone and Website, but I want to display all platforms. If there are no watch count, then it has to be displayed as 0 (zero). I also can’t display a final row which sums all watch_counts and displays a total.

SELECT sys_list_of_values.display_text, COUNT(history.history_id) AS 'total_watch' FROM history
JOIN sys_list_of_values ON sys_list_of_values.value_id = history.platform_id
WHERE history.video_id = 1
AND history.created <  '2012-05-07'
GROUP BY history.platform_id
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T07:04:06+00:00Added an answer on June 3, 2026 at 7:04 am

    Retaining your FROM HISTORY, change your JOIN to RIGHT JOIN

    SELECT sys_list_of_values.display_text, COUNT(history.history_id) AS 'total_watch' 
    FROM history
    RIGHT JOIN sys_list_of_values 
       ON sys_list_of_values.value_id = history.platform_id
       AND history.created <  '2012-05-07'
       AND history.video_id = 1
    
    GROUP BY sys_list_of_values.value_id 
    order by sys_list_of_values.display_text;
    

    If you want to use LEFT JOIN, switch the places of history and sys_list_of_values:

    SELECT sys_list_of_values.display_text, COUNT(history.history_id) AS 'total_watch' 
    FROM sys_list_of_values 
    LEFT JOIN history
       ON sys_list_of_values.value_id = history.platform_id
       AND history.created <  '2012-05-07'
       AND history.video_id = 1
    
    GROUP BY sys_list_of_values.value_id 
    order by sys_list_of_values.display_text;
    

    Live test: http://www.sqlfiddle.com/#!2/495d1/15


    GROUP BY with ROLLUP is incompatible with ORDER, anyway ROLLUP automatically sorts the list, just remove the ORDER:

    SELECT sys_list_of_values.display_text, 
      -- sys_list_of_values.value_id,
      COUNT(history.platform_id) AS 'total_watch' 
    FROM sys_list_of_values 
    LEFT JOIN history
       ON sys_list_of_values.value_id = history.platform_id
       AND history.created <  '2012-05-07'
       AND history.video_id = 1
    
    where sys_list_of_values.list_group = 'platform'
    GROUP BY 
     sys_list_of_values.display_text with rollup
    

    Live test: http://www.sqlfiddle.com/#!2/495d1/68

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have an intranet site that serves 50.000 users at maximum (generally only a
I have just installed dexterity into our office's intranet site so that I can
I have an intranet site that loads in IE7 compatibility mode, unless the user
I'm building a new Intranet Portal with SharePoint 2007 that will have News, Site
We're developing a site that will only run on the intranet, and computers with
I have a intranet site running PHP 5 that needs to list a folder
I have an ASP.net 2.0 intranet site that uses the indexing service on a
I have a public desktop site, a public mobile site, and a private intranet
I'm creating a little photo sharing site for our home's intranet, and I have
I have an Intranet http application running on several machines in our Windows domain;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.