本文介绍了Sql Server中分组行的平均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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


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),则只返回单行,其中包含所有行的平均值.

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.

我需要的是,首先我需要将它们按列分组,以便:
第2列的平均值,其中第1列= 1
第2列的平均值,其中第1列= 2
第2列的平均值,其中第1列= 3

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

所以我想返回3行,其中分别包含column1的各个值的平均值.我在这样做时迷失了,请提供任何提示/帮助吗?

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

ps:我尝试了几个相关问题,但没有一个帮助/我听不懂.

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

推荐答案

这是您想要的吗?

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

这篇关于Sql Server中分组行的平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 22:06