sql >> Base de Datos >  >> RDS >> Mysql

¿Cuál es el equivalente de SQL a pandas 'transformar'?

Puede unirse a una tabla derivada que contiene el valor agregado para cada agrupación de b

select * from mytable t1
join (
    select avg(a), b
    from mytable
    group by b
) t2 on t2.b = t1.b

o usando una subconsulta

select *, (select avg(a) from mytable t2 where t2.b = t1.b)
from mytable t1

la pregunta está etiquetada tanto mysql como psql, por lo que no estoy seguro de qué base de datos está utilizando. Pero en postgres puedes usar funciones de ventana

select *, avg(a) over (partition by b) 
from mytable