Sql Server中分组行的平均值

iam*_*ous 17 sql-server average

我有一个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  

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

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

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

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

Blo*_*ard 20

这是你想要的吗?

select column1, avg(column2) from table group by column1
Run Code Online (Sandbox Code Playgroud)