i have a simple table . How can i sum in SQL only values based on Riclass ID?
Riclass_Id | Value |
---|---|
0 | 92751 |
3 | 56757 |
4 | 64666 |
5 | 88888 |
I tried this code but doesn't work.
SELECT
Riclass_ID
SUM (Value)
FROM Mytable
WHERE Riclass_Id= 0 and Riclass_Id= 3
CodePudding user response:
Drop Riclass_ID
from Select
SELECT SUM (Value)
FROM Mytable
WHERE Riclass_Id in (0, 3)
and you'll get a single record - sum of all Value
s which belong to records with Riclass_Id
equals to either 0
or 3
CodePudding user response:
If you want the sum by Riclass_ID
you need use group by
clause : choose fields to be grouped
If you want only some Riclass_ID
, you need use where
clause : choose the value to be filtered
With group by
clause, you can select only :
- aggregat function (here
SUM(value)
, orcount(*)
, ormin/max
...) - fields specify in
group by
- const
With one (or more) aggregat function in the select
clause AND without group by
clause, you can't select fields without aggregat.
SELECT
Riclass_ID,
SUM (Value)
FROM
Mytable
WHERE
Riclass_Id in (0,3)
GROUP BY
Riclass_Id