Oracle
 SQL >> Base de Dados >  >> RDS >> Oracle

Calculando frequência de itens em colunas relacionadas

Para gravar a popularidade na tabela:

update my_table x
set popularity = (
  select count(distinct state)
  from my_table
  where fruit = x.fruit
);

Ou calcular em tempo de consulta com função analítica:

select state,
       fruit,
       count(distinct state) over (partition by fruit) as popularity
from my_table;

Isso retorna a quantidade de estados distintos por fruta.