我有2个表,用户和事件:
**Users:**
usersid
age
geo_country
gender
**adbreaks:**
ts
videoid
ads_watched
geo_country
userid
我的问题是:对于1月份的每周,请返回生成最多广告数量的10个视频。
我正在做这个代码吗?
SELECT video_id, COUNT(ads_watched) AS ad_watched
FROM (SELECT video_id, MAX(ads_watched) FROM adbreaks) adw
WHERE WEEK(ts) < WEEK('2017-02-01')
GROUP BY video_id
ORDER BY ad_watched DESC
LIMIT 10;
任何提示都将不胜感激。
谢谢,
和Claudia
答案 0 :(得分:0)
“对于1月的每个星期”,您是指每周1次查询还是整个月必须在结果中显示?如果在postgresql中预计每周1次查询:
SELECT video_id, COUNT(ads_watched) AS ad_watched
FROM adbreaks
WHERE EXTRACT(WEEK FROM ts) = 1 AND EXTRACT(YEAR FROM ts) = 2017
GROUP BY video_id
ORDER BY ad_watched DESC
LIMIT 10;
然后使用WHERE EXTRACT(WEEK FROM ts) = 2,3, and 4
运行查询
它将为您提供2017年1月的统计数据
如果您不关心这一年并希望累积所有可用数据,请删除年份约束
在mysql中:
SELECT video_id, COUNT(ads_watched) AS ad_watched
FROM adbreaks
WHERE WEEK(ts) = 1 AND YEAR(ts) = 2017
GROUP BY video_id
ORDER BY ad_watched DESC
LIMIT 10;