user3518835
user3518835

Reputation: 115

how to use case to select sum from table using multiple conditions in one column

I need to get sum of marks of level 4 and 5 from Results using a phone number as primary key .

The following way is the way it supposed to be but can not work with this i need to use case sql statement or anothe trick of sql if exists:

SELECT
 SUM(marks)
FROM Results 
 WHERE  phone=343435 AND  level='Level 4' AND level='Level  5'; 

Upvotes: 0

Views: 52

Answers (2)

Ullas
Ullas

Reputation: 11556

You could use an OR instead of AND like level='Level 4' OR level='Level 5'.

Query

SELECT SUM(marks)
FROM Results 
WHERE  phone=343435 AND  (level='Level 4' OR level='Level  5');

Or you can use an IN operator.

Query

SELECT SUM(marks)
FROM Results 
WHERE  phone=343435 AND  level IN('Level 4','Level  5');

Upvotes: 2

ClintB
ClintB

Reputation: 509

Try with an Or statement instead of and. You are saying the record's level field needs to have a value of 4 AND a value of 5. Instead of, give me rows where the VALUE =4 OR 5

SELECT
 SUM(marks)
FROM Results 
 WHERE  phone=343435 AND  (level='Level 4' OR level='Level  5');

Upvotes: 0

Related Questions