(If you wanted to group by more than one
field, it would be in the order specified by the group parameter.)
Depending on your data, your results would look something like this:
Average Accident Count 3.000 for age 18
Average Accident Count 2.020 for age 19
Average Accident Count 1.010 for age 20
. . .
However, for more complex queries, you may need to craft a SQL statement manually.
Specifically, you can use find_by_sql to search for any arbitrary SQL query, and this
allows you to ask for virtually any type of information. For example, if a vehicle_model
column existed in your Accident model, you could group by vehicle_model and run two
aggregate functions: average age and average accident_count. This would give you an
average owner age and an average number of accidents per vehicle, so you could get
some idea whether, say, a Ford Explorer was a safer ride than a Honda CRX. (Of course,
driver control would play a significant part, and hence the average age is a helpful statistic.)
You can??™t use multiple aggregate functions with calculate, but you can with
find_by_sql, like this:
sql = "SELECT vehicle_model,
AVG(age) as average_age,
AVG(accident_count) AS average_accident_count
FROM persons
GROUP
BY vehicle_model
"
Person.
Pages:
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58