I have a column in my table that has values of type FLOAT. How can I get the average value of all items in this column?
4 Answers
select avg( columnname) from table;
This will average all rows. To average a subset, use a where clause. To average for each group (of something) use a group by clause.
2 Comments
LarsH
Can you give an example with a
where clause inside the avg()? When I try it in SQLite, I get a syntax error. Or does the where clause go outside the avg()? In that case, it would seem ambiguous... filtering the SELECT instead of the AVG().alttag
@LarsH: e.x.,
SELECT AVG(colname) FROM table WHERE otherCol = 'foo'; If it helps, think of the WHERE clause as being executed first, then the aggregate function AVG() operates on whatever rows remain.