user8318984
user8318984

Reputation:

SQL Sum values in table when it encounters a specific conditions

I would like to sum the result set from a table when it matches a conditions

Suppose, the table contains below data

ID PILLER AMOUNT
1  1M     10000
2  2M     15000
3  1M     10000
4  3W     50000
5  1M     10000

Now, from the table rows I would like to sum the amount of 1M which appears 3 time to one row.

Upvotes: 0

Views: 42

Answers (2)

Himanshu
Himanshu

Reputation: 3970

Is this what you want ?
In case there are multiple pillars associated to an id

 Select 
 ID,PILLER,Sum(AMOUNT) 
 from table where piller 
 in ('2W','3W','1M') 
 group 
 by ID,PILLAR ;

or only pillarwise sum

  Select 
  PILLER,Sum(AMOUNT) 
 from table where piller 
 in ('2W','3W','1M') 
 group 
 by PILLAR ;

Upvotes: 1

Zaynul Abadin Tuhin
Zaynul Abadin Tuhin

Reputation: 31993

use in operator in where clause

   select sum(amount) from table
    where piller in ('2W','3W','1M')

Upvotes: 0

Related Questions