MySQL: How do I get the AVG value in one column for entries sharing distinct values in another column? -
in case of table such:
id num grp 1 12.3 group1 2 2.4 group2 3 7.6 group2 4 4.5 group1 5 8.9 group1 6 12.6 group1 7 1.1 group2 8 7.8 group1
i output result grid additional column have value of 9.22 in rows grp value of group1, , 3.7 in rows grp value of group2. so, in each case, average num value of rows same grp value.
i'm still learning mysql. think know components need (avg, distinct/group by, , possibly self join?), i'm struggling find right way put them together.
can please try query below, using group by
of avg
, join
same table, can column value expectation:
select ta.id, ta.num, ta.grp , ag.avgnum ( select grp, avg(num) avgnum tablename group grp ) ag join tablename ta on ta.grp = ag.grp
rextester demo: http://rextester.com/szw95037
Comments
Post a Comment