我正在记录用户观看一系列视频的次数。现在我正在尝试制作每天观看任何视频的用户数量的图表。
UserVideoWatching.where("created_at >= ? AND user_id != ?",1.month.ago, User.elephant.id).group("DATE(created_at)").reorder('created_at').count
产生 sql
SELECT COUNT(*) AS count_all, DATE(created_at) AS date_created_at FROM `user_video_watchings` WHERE (created_at >= '2013-01-27 10:43:24' AND user_id != 7) GROUP BY DATE(created_at) ORDER BY created_at
这会为每天观看的所有视频产生正确的结果,但正如我所说,我只想向每个用户显示一次。
我想要的sql是
SELECT COUNT(DISTINCT user_id) AS count_all, DATE(created_at) AS date_created FROM `user_video_watchings` WHERE (created_at >= '2013-01-27 10:33:18' AND user_id != 7) GROUP BY DATE(created_at) ORDER BY created_at
所以我认为
UserVideoWatching.where("created_at >= ? AND user_id != ?",1.month.ago, User.elephant.id).group("DATE(created_at)").reorder('created_at').select('COUNT(DISTINCT user_id) AS count_all, DATE(created_at) AS date_created')
会做我想做的事。但这给了
[#<UserVideoWatching >, #<UserVideoWatching >]
而不是哈希。
有任何想法吗?
我正在使用 rails 3.1 和 mysql