我想对Useractivities进行两次查询,其中一个是我在工作时间内找到所有条目(mon-fri 08-16),另一个是我在这个时间段之外找到所有条目。
Useractivity.where('activity = ? AND created_at >= ? AND EXTRACT (HOUR from created_at) BETWEEN ? AND ?', 'login',Time.now.since(-7.days).to_date, 8,16).count
Useractivity.where('activity = ? AND created_at >= ? AND EXTRACT (HOUR from created_at) NOT BETWEEN ? AND ?', 'login',Time.now.since(-7.days).to_date, 8,16).count
我如何排除第一个查询的星期六和星期日,并在第二个查询中包含它们(所有时间)?
答案 0 :(得分:2)
在WHERE子句中添加一个额外条件。
工作日是:
... AND EXTRACT(dow FROM created_at) BETWEEN 1 AND 5 ...
周末:
... AND EXTRACT(dow FROM created_at) NOT BETWEEN 1 AND 5 ...