ViruMax
ViruMax

Reputation: 1218

group by clause is not producing the expected sum

I am developing iPhone applications and using sqlite database.

I am having answers_ELA table as follows:

Id    | observation_id    | question_no    | answer    | answer2
-------------------------------------------------------------------
1     | 60                | 0              | 2         | 1
2     | 60                | 1              | 3         | 4
3     | 61                | 0              | 3         | 4
4     | 61                | 1              | 3         | 2
5     | 62                | 0              | 3         | 1
6     | 62                | 1              | 1         | 2

And I want result

question_no    | total
-------------------------
0              | 14
1              | 15

But its giving

question_no    | total
-------------------------
0              | 4
1              | 3

Means for last observation_id only i.e. 62

And the query is as follows:

SELECT 
    question_no,
    (answer+answer2) total
FROM 
    answers_ELA 
WHERE
    observation_id IN(60,61,62) 
GROUP BY 
    question_no

What's wrong in this, and what should I do to get desired3 result.

Upvotes: 1

Views: 76

Answers (3)

DINESH4DINA
DINESH4DINA

Reputation: 45

Guess, this one will work for you...

SELECT question_no, 
       Sum(answer + answer2) total 
FROM   answers_ela 
WHERE  observation_id IN( 60, 61, 62 ) 
GROUP  BY question_no
ORDER  BY question_no;

Upvotes: 0

magic_al
magic_al

Reputation: 2180

You have no aggregation function assigned. Try SUM(answer+answer2).

Upvotes: 1

Saharsh Shah
Saharsh Shah

Reputation: 29051

Try this:

SELECT question_no, SUM(answer+answer2) total
FROM answers_ELA 
WHERE observation_id IN (60,61,62) 
GROUP BY question_no

Upvotes: 1

Related Questions