insert into analytics.ld_cat_det (date, content_id, category_id, parent_category_id, content_views, video_views, picture_views, audio_views); select a.stats_day date ,a.content_id ,c.category_id ,d.parent_category_id ,sum(a.unique_views) as content_views ,sum(if(b.content_type='VIDEO',a.unique_views,0)) video_views ,sum(if(b.content_type='PICTURE',a.unique_views,0)) picture_views ,sum(if(b.content_type='AUDIO',a.unique_views,0)) audio_views from virv.CONTENT_VIEW_STATS a, virv.CONTENT b, (select content_id, category_id from virv.CONTENT_CATEGORIZATION group by content_id)c, virv.CATEGORIES d where a.content_id = b.content_id and a.content_id = c.content_id and b.content_id = c.content_id and c.category_id = d.category_id and date(a.stats_day) = subdate(curdate(),1) group by a.stats_day ,a.content_id ,c.category_id ,d.parent_category_id