Sql Server中分组行的平均值

时间:2021-06-01 22:46:17

I have an Sql Server Table.. it's something like this:

我有一个Sql Server表..它是这样的:

Id ...... Column1 ...... Column2  
````````````````````````````````  
1 ........ 1 ............. 34  
2 ........ 1 ............. 44  
3 ........ 2 ............. 45  
4 ........ 2 ............. 36  
5 ........ 2 ............. 23  
6 ........ 3 ............. 68  
7 ........ 3 ............. 26  

So, I need to select the average of Column2,but group with column1 before doing that.
I mean, if I say Avg(Column2) it just returns a single row with the average of all rows.

因此,我需要选择Column2的平均值,但在执行此操作之前,请选择column1的列。我的意思是,如果我说Avg(Column2)它只返回一行与所有行的平均值。

What I need is, first i need to group them by column so:
Average of column2 where column1 = 1
Average of column2 where column1 = 2
Average of column2 where column1 = 3

我需要的是,首先我需要按列对它们进行分组,以便:column2的平均值,其中column1 = 1 column2的平均值,其中column1 = 2 column2的平均值column1 = 3

So I want 3 rows returned with the averages of respective values of column1. I am lost at doing this, any hints / help please?

所以我希望返回3行,其中包含column1各自值的平均值。我迷失了这个,有任何提示/帮助吗?

ps: I tried several related questions, and none of them helped / I couldn't understand.

ps:我尝试了几个相关的问题,但没有一个帮助/我无法理解。

6 个解决方案

#1


21  

Is this what you want?

这是你想要的吗?

select column1, avg(column2) from table group by column1

#2


4  

simple

简单

select AVG(Column2) from table group by Column1

doesn't work?

不起作用?

#3


2  

SELECT column1, AVG(column2) 
  FROM "Insert table name"
GROUP BY column1 

#4


1  

SELECT Column1, AVG(Column2) FROM test GROUP BY Column1;

#5


1  

This following Query will help for Calculate the average value of a ROW:

以下查询将有助于计算ROW的平均值:

 select Avg(A.Tamil + A.English + A.Maths + A.Science + A.Social_Science)/5 As 
 Average
 from MarkTable A, MarkTable B, MarkTable C
 where A.RollNo='14UCA002'

This might helpful...

这可能有帮助......

#6


0  

Execute the following SQL from average :

从平均值执行以下SQL:

select column1,avg(column2) from tablename group by column1;

#1


21  

Is this what you want?

这是你想要的吗?

select column1, avg(column2) from table group by column1

#2


4  

simple

简单

select AVG(Column2) from table group by Column1

doesn't work?

不起作用?

#3


2  

SELECT column1, AVG(column2) 
  FROM "Insert table name"
GROUP BY column1 

#4


1  

SELECT Column1, AVG(Column2) FROM test GROUP BY Column1;

#5


1  

This following Query will help for Calculate the average value of a ROW:

以下查询将有助于计算ROW的平均值:

 select Avg(A.Tamil + A.English + A.Maths + A.Science + A.Social_Science)/5 As 
 Average
 from MarkTable A, MarkTable B, MarkTable C
 where A.RollNo='14UCA002'

This might helpful...

这可能有帮助......

#6


0  

Execute the following SQL from average :

从平均值执行以下SQL:

select column1,avg(column2) from tablename group by column1;