postgresql SQL中两列的平均值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21213677/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 01:16:27  来源:igfitidea点击:

Average of two columns in SQL

sqldatabasepostgresqlaverage

提问by AKIWEB

I wanted to take average of two columns and display in a new column something like this:

我想取两列的平均值并在一个新列中显示如下:

+-+--+--+--+--------+--------+
|A|B |C |D |AVG(B/C)|AVG(C/B)|
+-+--+--+--+--------+--------+
|S|23|34|56|        |        |
+-+--+--+--+--------+--------+
|T|45|6 |79|        |        |
+-+--+--+--+--------+--------+

So, as shown above, I needed to take the each row values and perform B/C and then take the average accordingly to display it in a new column.

因此,如上所示,我需要取每行值并执行 B/C,然后相应地取平均值以将其显示在新列中。

I wanted to do this in SQL query. Is it possible to perform this in a SQL command? I know the AVG()function does take the average of a column but how can I do B/Cand then take the average? also if I need to take the average of B and C as well how can i do that.

我想在 SQL 查询中做到这一点。是否可以在 SQL 命令中执行此操作?我知道该AVG()函数确实取了一列的平均值,但我该怎么做B/C然后取平均值?另外,如果我还需要取 B 和 C 的平均值,我该怎么做。

This is what I am doing right now:

这就是我现在正在做的事情:

Select A,B,C,D FROM tableTest where A='S';

I now have to take the average of corresponding and also have another two columns additionally in the query to show the respective results.

我现在必须取相应的平均值,并且在查询中还有另外两列以显示相应的结果。

采纳答案by MarcinJuraszek

You should give it a try:

你应该试一试:

SELECT A, AVG((B+C)/2) as bc, AVG((C+B)/2) as cb
FROM tableTest
WHERE A = 'S'
GROUP BY A