我正在尝试按商业周获取电话数量。我已经写了sql:
select strftime('%W', date_time_origination) as week, count(*)
from calls
group by week;
然后我尝试在rails中运行它,首先使用这个:
calls_by_week = Call.select("strftime('%W',date_time_origination) as week, count(*) as total_number_of_calls").group("week")
这返回了具有正确数据但重复多次的对象集合(json 表示):
[{ "total_number_of_calls": 5435, "week": 39 },.....]
鉴于它应该返回一个对象,我尝试更改 rails 查询结构:
calls_by_week = Call.select("strftime('%W', date_time_origination) as week").group("strftime('%W', date_time_origination)").count
这一次,查询返回一个哈希,减去命名参数:
{ "31": 3123, "32": 1231,... }
我希望将数据以 json 格式显示如下:
{ "week": 31, "total_number_of_calls": 12412,.....}
有没有办法消除第一个查询产生的重复?