PostgreSQL
 sql >> Datenbank >  >> RDS >> PostgreSQL

PostgreSQL findet gemeinsame Kombination von Paaren in derselben Spalte

Ich denke, Sie brauchen einen Join, um alle Plattformpaare zu finden:

select 
  sp1.platform as platform1, sp2.platform as platform2, count(*) as numcommon 
from 
  seriesonplatform AS sp1
  JOIN seriesonplatform AS sp2 ON (sp1.seriesid = sp2.seriesid) AND (sp1.platform <>  sp2.platform)
where 
  sp1.platform in ('BET','BET+')
  AND sp2.platform in ('BET','BET+')
group by
  sp1.platform,
  sp2.platform

BEARBEITEN 2

Mit der obigen Abfrage können Sie die gemeinsame Seriennummer finden. Alles, was übrig bleibt, ist den Prozentsatz zu finden:

SELECT 
  platform1.platform, common.platform2, numcommon * 1.0 / platform1.seriescount AS show_on_platform2
FROM (
  SELECT 
    platform, count(*) AS seriescount
  FROM 
    seriesonplatform
  GROUP BY
    platform 
) platform1
LEFT JOIN (
  SELECT 
    sp1.platform as platform1, sp2.platform as platform2, count(*) as numcommon 
  FROM 
    seriesonplatform AS sp1
    JOIN seriesonplatform AS sp2 ON (sp1.seriesid = sp2.seriesid) AND (sp1.platform <>  sp2.platform)
  GROUP BY
    sp1.platform,
    sp2.platform
) AS common ON platform1.platform = common.platform1
WHERE 
  numcommon * 1.0 / platform1.seriescount > 0.5
ORDER BY
  platform1.platform, platform2