18

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  

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.

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

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: I tried several related questions, and none of them helped / I couldn't understand.

Tom H
  • 46,766
  • 14
  • 87
  • 128
iamserious
  • 5,385
  • 12
  • 41
  • 60

6 Answers6

23

Is this what you want?

select column1, avg(column2) from table group by column1
Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
4

simple

select AVG(Column2) from table group by Column1

doesn't work?

nothrow
  • 15,882
  • 9
  • 57
  • 104
2
SELECT column1, AVG(column2) 
  FROM "Insert table name"
GROUP BY column1 
ahsteele
  • 26,243
  • 28
  • 134
  • 248
Baaju
  • 1,992
  • 2
  • 18
  • 22
1
SELECT Column1, AVG(Column2) FROM test GROUP BY Column1;
user373455
  • 12,675
  • 4
  • 32
  • 46
0

This following Query will help for Calculate the average value of a 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...

Ismayil S
  • 223
  • 3
  • 20
-1

Execute the following SQL from average :

select column1,avg(column2) from tablename group by column1;
Ori Marko
  • 56,308
  • 23
  • 131
  • 233