user3358 user3358 - 7 months ago 9
SQL Question

Querying the sum for each value in column

I have the table with values as shown in below table:

book table

And I would like to get results as in below table:

results

I'm trying to query sum of ans1 like this for all rows

`SELECT sub_id, SUM(ans1) FROM booktable WHERE book_id =1 AND sub_id = 4 OR sub_id = 6`


But I'm not getting the expected result as shown in above table, instead I'm getting result for only one value in sub_id column. I'm a beginner in sql, please help. Thanks.

Answer
SELECT sub_id, SUM(ans1) FROM booktable
 WHERE book_id = 1 AND sub_id IN (4, 6)
Group by sub_id
Comments