我必须将用sql编写的查询翻译成ruby(activeRecord / Linq),但我找不到正确的语法,请帮忙。
我想知道在节日中进行这4项活动的独特用户数量。 sql查询如下:
`SELECT count(distinct user) FROM stats where festivalId='fest000'
and user in (select user from stats where activity='checkins')
and user in (select user from stats where activity='programs')
and user in (select user from stats where activity='status')
and user in (select user from stats where activity='pictureUpload');`
我怎么能设法做到这一点? 我在哪里可以找到至少类似的例子或良好的文档来解决它? 是否可以使用常量“用户”,以便我可以执行所有查询然后将它们放在一起或类似的东西?
我很绝望,我找不到解决办法,我不知道该怎么做......先谢谢!!
答案 0 :(得分:0)
我想你需要找出具有festivalId='fest000'
和activity 'checkins' AND 'programs' AND 'status' AND'pictureUpload')
然后下面的查询应该有效
SELECT COUNT(*) FROM stats
WHERE festivalId='fest000' AND activity IN ('checkins', 'programs', 'status','pictureUpload')
GROUP BY user
HAVING COUNT(DISTINCT activity) = 4
您也可以轻松地以ruby方式转换此查询
Stat.where("festivalId=? AND activity IN (?)", 'fest000', ['checkins', 'programs', 'status','pictureUpload']).group('user').having("COUNT(distinct activity) = 4").count