How to SELECT sum of numbers less than 30 and sum of numbers greater than 30? How to SELECT sum of numbers less than 30 and sum of numbers greater than 30? oracle oracle

How to SELECT sum of numbers less than 30 and sum of numbers greater than 30?


Something along these lines (correct the syntax for your environment):

SELECT    sum(case when Field < 30 then Field else 0 end) as LessThan30,    sum(case when Field > 30 then Field else 0 end) as MoreThan30FROM    DaTable


The requirement is a big vague, but I'll assume SUM across rows and <> 30. This is different to the case answer.

SELECT    SelectCol1, SelectCol2, SelectCol3, ..., SUM(AggregateCol)FROM    TableGROUP BY    SelectCol1, SelectCol2, SelectCol3, ...HAVING    SUM(AggregateCol) <> 30


If i understand correctly, you have a single column which has numeric values and you want to find sum of this field for all thw rows where the field value is less than 30 and same condition with the variation of field value greater than 30. If this is correct, then you can use the below query and replcae the table and column name accordingly.

SELECT  SUM(CASE        WHEN col1 < 30 THEN COL1        ELSE 0 END       )    SUM(CASE        WHEN col1 < 30 THEN COL1        ELSE 0 END       ) FROM   DATABASE.SCHEMANAME.TABLE

Thanks,Atul