user3358 user3358 - 2 years ago 82
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:


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 Source
SELECT sub_id, SUM(ans1) FROM booktable
 WHERE book_id = 1 AND sub_id IN (4, 6)
Group by sub_id
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download