使用SQL中的WHERE子句进行求和

huangapple go评论68阅读模式
英文:

Sum using where clause in SQL

问题

以下是翻译好的部分:

我正在尝试根据一个单独的列中的文本是否符合某些条件来对该列中的值进行求和。我正在尝试使用Where子句来实现这一目标,但一直得到0作为答案。

表格

| 人员 | 分数 |
| -------- | -------- |
| A   | 70   |
| B   | 85   |
| C   | 50   |
| D   | 100   |
| E   | 20   |

我想要得到人员 A、D、E 的分数列总和。我期望得到如下结果:

| Sum(分数) 
| -------- |
| 190   |

以下是我正在使用的表达式,其中总和错误地返回为0

SELECT SUM (分数) 
FROM 表*
WHERE 人员 = 'A' OR 'D' OR 'E';
英文:

I am trying to Sum the value in a certain column, based upon if the text in a separate column matches certain criteria. I am trying to use the Where clause to do this, and keep getting 0 as an answer.

Table

Person Score
A 70
B 85
C 50
D 100
E 20

I want to get a Sum of the Score Column for Persons A, D, E. I expect to get this

Sum(Score)
190

Here is the expression I am using, where the Sum is incorrectly returned as 0

SELECT SUM (Score) 
FROM T*
WHERE Person = 'A' OR 'D' OR 'E';

答案1

得分: 3

问题出在你的 where 语句,当使用 OR 时,你必须再次输入列名,所以你可以这样做:

WHERE Person = 'A' OR Person = 'D' OR Person = 'E';

或者更好的方法是使用 IN

WHERE Person IN ('A', 'D', 'E');
英文:

The problem is your where, when using OR you must type the column name again, so you could do:

WHERE Person = 'A' OR Person = 'D' OR Person = 'E';

or do better, using IN:

WHERE Person IN ('A', 'D', 'E');

答案2

得分: 0

似乎你正在寻找IN ()操作符:

SELECT SUM (Score) 
FROM T*
WHERE Person IN ('A', 'D', 'E');
英文:

Seems like your looking for the IN () operator:

SELECT SUM (Score) 
FROM T*
WHERE Person IN ('A', 'D', 'E');

答案3

得分: 0

尝试这个:

SELECT person, SUM(Score) 
FROM T
WHERE Person = 'A' OR Person = 'D' OR Person = 'E'
GROUP BY 
person

你还可以将这些 'or' 改为上面提到的 'in' 结构。

英文:

Try this one:

SELECT person, SUM (Score) 
FROM T
WHERE Person = 'A' OR 'D' OR 'E'
Group by 
person

You could also change this 'or's to presented above 'in' structure

huangapple
  • 本文由 发表于 2023年2月24日 00:55:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75547939.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定