Kevin Houde
Kevin Houde

Reputation: 319

Cannot get SUM to work in mysql

I need to have this

  GROUP_ID     TOTALSUM       
     1          67,40000000
     2          56,5454

With this query

 select 
     exam.id, exam.ponderation / totalponderation *  avg(scores.result) as totalsum
 from 
    scores, exam,
    (SELECT SUM(ponderation) AS totalponderation
     FROM exam) AS sumponderation
 where 
     exam.group_id in (91, 93) and exam.id = scores.exam_id
 group by 
     exam.name

I tried lots of queries, but nothing works.

Query #1 (doesn't work) (query error)

select sum(exam.ponderation / totalponderation *  avg(scores.result)) as totalsum

Query #2 (doesn't work) (it returns 251 as total)

 select sum( exam.ponderation / totalponderation *  avgscores )
 from scores, exam,
      (SELECT SUM(ponderation) AS totalponderation
       FROM exam) AS sumponderation,
      (SELECT avg(scores.result) AS avgscores
       FROM scores, exam
       where exam.group_id in(91,93)  and exam.id = scores.exam_id) AS avgponderation

So... On Can I do a Sum of my column ?

EDIT

My exam table

id----name-----ponderation----group_id---subject_id----date-
1------test1----------150----------4 ------------4-------------2011-11-11
2------test2----------20----------4 ------------4-------------2011-11-11
3-------test3---------20---------3--------------4-------------2011-11-11

My scores table

id----exam_id-----user_id----subject_id result-------------date------order
1------1------------5-------------4 ------------80-------------2011-11-11-------1
2------2------------25-------------4 ------------30-------------2011-11-11------0
3------1------------5-------------4 ------------61-------------2011-11-11-------1
4------2------------25-------------4 ------------80-------------2011-11-11------0

edit: I need to group BY group_id

  GROUP_ID     TOTALSUM       
         1          67,40000000
         2          56,5454

Thx

Upvotes: 0

Views: 184

Answers (1)

Conrad Frix
Conrad Frix

Reputation: 52645

Perhaps calculating the avgscores in another inline view before using it may solve your problems

SELECT 
e.group_id,
   SUM(e.ponderation /  totalponderation *  avgscores)  TOTAL_sum

FROM 
    exam e

    INNER JOIN (SELECT exam_id, avg(scores.result) avgscores 
         FROM scores
      GROUP BY exam_id) a
    ON e.id = a.exam_id,
    (SELECT SUM(ponderation) AS totalponderation
      FROM exam) AS sumponderation
Group by 
   e.group_id

Upvotes: 2

Related Questions