ronquiq
ronquiq

Reputation: 2727

SQL Count Average

I have table like

id     userid  semid  courseid  coursename  total
1       36      17       13       CA         23
2       36      17       5        CB         46        
3       36      17       8        CC         20
4       36      19       16       CD         34
5       36      19       13       CA         31
6       36      19       3        CA#        29
7       36      19       7        CE         60
8       36      10       9        CK         32
9       36      10       15       CH         56    

I need average of semid for a userid i.e., SUM(courseid) /count (moduleid), It was showing 9 as module count, but I have only 3 modules. This is my query

SELECT userid, SUM(total)/count(semid) FROM custom WHERE userid=36  

Upvotes: 0

Views: 220

Answers (3)

John Woo
John Woo

Reputation: 263943

just use the AVG( ) function

SELECT   userid, semid, AVG(total)
FROM     custom
WHERE    userid = 36
GROUP BY userid, semid

SQLFiddle Demo

Upvotes: 2

JDGuide
JDGuide

Reputation: 6525

There is MYSQL aggregate function AVG() for finding Average . @John Totet Woo has posted the answer.

Upvotes: 0

sundar
sundar

Reputation: 1770

SELECT userid, SUM(total)/count(distinct semid) FROM custom WHERE userid=36   

Try this query

Upvotes: 0

Related Questions