7

In my table I have some colms like this, (beside another cols)

col1 | col2
s1   |  5
s1   |  5
s2   |  3
s2   |  3
s2   |  3
s3   |  5
s3   |  5
s4   |  7

I want to have average of ALL col2 over Distinct col1. (5+3+5+7)/4=5

BlueGirl
  • 491
  • 2
  • 9
  • 29
  • 1
    Can you share the output you're expecting to get for this data? Your calculation and your text seem to contradict each other. – Mureinik Oct 15 '14 at 07:59

5 Answers5

8

Try this:

SELECT AVG(T.col2)
FROM 
    (SELECT DISTINCT col1, col2
    FROM yourtable) as T
Joe Taras
  • 15,166
  • 7
  • 42
  • 55
5

You are going to need a subquery. Here is one way:

select avg(col2)
from (select distinct col1, col2
      from my_table
     ) t
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
1

Average is the sum of a set of values divided by the number of those value. Here, you want to divide by a count of the distinct values, so:

SELECT SUM(col2)/COUNT (DISTINCT col1)
FROM   my_table
Mureinik
  • 297,002
  • 52
  • 306
  • 350
0

You can just say

SELECT col1, AVG( col2 ) FROM my_table GROUP BY col1
fallenland
  • 525
  • 2
  • 6
-1

Try

SELECT AVG(coll2) as Average From myTable Group by Col1,Col2
Nuru Salihu
  • 4,756
  • 17
  • 65
  • 116