2015-11-19 8 views
8

Sto costruendo un'analisi di rete utilizzando D3.js per mostrare i numeri di telefono connessi all'interno della mia app fino a sei gradi di separazione. L'SQL (postgres) per trovare le connessioni iniziali è al di sotto e abbastanza semplice. Tuttavia, sono perplesso su come modificare questo per attraversare sei livelli di connessioni e poi fermarsi.Query SQL 6 gradi di separazione per l'analisi di rete

SELECT player_id, ps.player_state, ps.email, ph.create_date 
FROM game.phone_hashes ph 
INNER JOIN game.customer_settings cs ON cs.id = ph.player_id 
WHERE hash IN (SELECT hash FROM game.phone_hashes WHERE player_id = $1); 

ho trovato menzioni di espressioni di tabella comuni (CTE) e ricorsione attraverso la ricerca in questo problema, ma sono incerto come applicarli qui.

Quello che sto puntando è ottenere tutti i giocatori connessi al giocatore iniziale ($ 1) tramite un comune hash del telefono, quindi tutti i giocatori connessi a ciascuna di queste connessioni tramite un hash del telefono comune, e così via. fino a 6 gradi di separazione.

+7

Si prega di modificare la tua domanda e aggiungere la definizione ('create table') delle tabelle coinvolte. –

+0

Inoltre, spesso aiuta a capire la domanda se la domanda include un esempio: poche righe di dati e quale dovrebbe essere il risultato della query sulla base di questi dati di esempio. –

+0

Una ** definizione tabella ** è ciò che si ottiene con '\ d game.phone_hashes' in psql. O lo script completo 'CREATE TABLE'. –

risposta

7

Penso che questo sia quello che volevi dire:

with recursive tc as(
select $1 as player_id, 1 as level 
    union 
select ph2.player_id, level+1 
    from tc, phone_hashes ph1, phone_hashes ph2 
    where tc.player_id=ph1.player_id 
    and ph1.hash=ph2.hash 
    and tc.level < 6 
)  
select distinct player_id from tc 
4

penso che sarebbe:

-- 6 degrees of separation 
SELECT player_id, ps.player_state, ps.email, ph.create_date 
FROM game.phone_hashes ph 
INNER JOIN game.customer_settings cs ON cs.id = ph.player_id 
WHERE hash IN 
(SELECT hash FROM game.phone_hashes 
WHERE hash IN 
(SELECT hash FROM game.phone_hashes 
    WHERE hash IN 
    (SELECT hash FROM game.phone_hashes 
    WHERE hash IN 
    (SELECT hash FROM game.phone_hashes 
    WHERE hash IN 
    (SELECT hash FROM game.phone_hashes 
    WHERE hash IN 
    (SELECT hash FROM game.phone_hashes 
     WHERE player_id = $1)))))); 

Si prega di vedere lavorazioni di seguito:

-- 1 degree of separation 
SELECT player_id, ps.player_state, ps.email, ph.create_date 
FROM game.phone_hashes ph 
INNER JOIN game.customer_settings cs ON cs.id = ph.player_id 
WHERE hash IN 
(SELECT hash FROM game.phone_hashes WHERE player_id = $1); 

-- 2 degrees of separation 
SELECT player_id, ps.player_state, ps.email, ph.create_date 
FROM game.phone_hashes ph 
INNER JOIN game.customer_settings cs ON cs.id = ph.player_id 
WHERE hash IN 
(SELECT hash FROM game.phone_hashes 
WHERE hash IN 
(SELECT hash FROM game.phone_hashes 
    WHERE player_id = $1)); 

-- 3 degrees of separation 
SELECT player_id, ps.player_state, ps.email, ph.create_date 
FROM game.phone_hashes ph 
INNER JOIN game.customer_settings cs ON cs.id = ph.player_id 
WHERE hash IN 
(SELECT hash FROM game.phone_hashes 
WHERE hash IN 
(SELECT hash FROM game.phone_hashes 
    WHERE hash IN 
    (SELECT hash FROM game.phone_hashes 
    WHERE player_id = $1))); 

-- 4 degrees of separation 
SELECT player_id, ps.player_state, ps.email, ph.create_date 
FROM game.phone_hashes ph 
INNER JOIN game.customer_settings cs ON cs.id = ph.player_id 
WHERE hash IN 
(SELECT hash FROM game.phone_hashes 
WHERE hash IN 
(SELECT hash FROM game.phone_hashes 
    WHERE hash IN 
    (SELECT hash FROM game.phone_hashes 
    WHERE hash IN 
    (SELECT hash FROM game.phone_hashes 
    WHERE player_id = $1)))); 


-- 5 degrees of separation 
SELECT player_id, ps.player_state, ps.email, ph.create_date 
FROM game.phone_hashes ph 
INNER JOIN game.customer_settings cs ON cs.id = ph.player_id 
WHERE hash IN 
(SELECT hash FROM game.phone_hashes 
WHERE hash IN 
(SELECT hash FROM game.phone_hashes 
    WHERE hash IN 
    (SELECT hash FROM game.phone_hashes 
    WHERE hash IN 
    (SELECT hash FROM game.phone_hashes 
    WHERE hash IN 
    (SELECT hash FROM game.phone_hashes 
    WHERE player_id = $1))))); 

-- 6 degrees of separation 
SELECT player_id, ps.player_state, ps.email, ph.create_date 
FROM game.phone_hashes ph 
INNER JOIN game.customer_settings cs ON cs.id = ph.player_id 
WHERE hash IN 
(SELECT hash FROM game.phone_hashes 
WHERE hash IN 
(SELECT hash FROM game.phone_hashes 
    WHERE hash IN 
    (SELECT hash FROM game.phone_hashes 
    WHERE hash IN 
    (SELECT hash FROM game.phone_hashes 
    WHERE hash IN 
    (SELECT hash FROM game.phone_hashes 
    WHERE hash IN 
    (SELECT hash FROM game.phone_hashes 
     WHERE player_id = $1))))));