用戶按年齡我想按年齡範圍列出用戶數:集團紅寶石
Range : #Users
10-14 : 16
15-21 : 120
22-29 : 312
30-40 : 12131
41-70 : 612
71-120 : 20
我想創建散列的靜態數組:
AGE_RANGES = [
{label:"10 - 14", min:10, max:14},
{label:"15 - 21", min:15, max:21},
{label:"22 - 29", min:22, max:29},
{label:"30 - 40", min:30, max:40},
{label:"41 - 70", min:41, max:70},
{label:"71 - 120", min:71, max:120}
]
然後將其用於我的搜索過濾器以及我的查詢。但是,我想不出一種從中獲得最高性能的方法。
我在我的模型方法只按年齡組:
def self.group_by_ageRange(minAge, maxAge)
query = User.group("users.age")
.where("users.age BETWEEN minAge and maxAge ")
.select("users.age,
count(*) as number_of_users")
end
有什麼建議?
http://stackoverflow.com/questions/232387/in-sql-how-can-you-group-by-in-ranges – InternetSeriousBusiness