sql - How to count common neighbors of two users and calculate similarity? -
the monthly_connections table contains columns calling_party, called_party, common_neighbors, neighborhood_overlap
so table describes users connected. 1 of measures user similarity neighborhood overlap defined following:
neighborhood_overlap = (number of nodes neighbors of both calling_party , called_party)/(number of nodes neighbors of @ least 1 of calling_party or called_party)
trying calculate number of common neighbors 2 users wrote following query:
select count (*) (select t1.neighbora ( select called_party neighbora monthly_connections calling_party = '9f7334bcf9000cd68d40302dc4801e60c027a7d1' union select calling_party neighbora monthly_connections called_party = '9f7334bcf9000cd68d40302dc4801e60c027a7d1') t1 inner join (select called_party neighborb monthly_connections calling_party = '10d149a4356e1aa3a8af604bd992bba141db53d2' union select calling_party neighborb monthly_connections called_party = '10d149a4356e1aa3a8af604bd992bba141db53d2') t2 on t1.neighbora = t2.neighborb) t3
the query above calculates number of common neighbors of users 10d149a4356e1aa3a8af604bd992bba141db53d2 , 9f7334bcf9000cd68d40302dc4801e60c027a7d1
the goal write query set value of column common neighbors , neighborhood overlap each pair of connection in table
does know how write query update columns common_neighbors , neighborhood_overlap?
for common neighbors started write following query not correct:
update mc set common_neighbors = (select count (*) (select t1.neighbora (select called_party neighbora monthly_connections calling_party = mc.calling_party union select calling_party neighbora monthly_connections called_party = mc.calling_party) t1 inner join (select called_party neighborb monthly_connections calling_party = mc.called_party union select calling_party neighborb monthly_connections called_party = mc.called_party) t2 on t1.neighbora = t2.neighborb) t3) monthly_connections mc inner join t3 on t3.calling_party = mc.calling_party , t3.called_party = mc.called_party
i think query works (though might not performant).
update mc set common_neighbors = (select count (*) ( (select called_party monthly_connections calling_party = mc.calling_party union select calling_party monthly_connections called_party = mc.calling_party ) intersect (select calling_party monthly_connections called_party = mc.called_party union select called_party monthly_connections calling_party = mc.called_party ) ) t1 ) monthly_connections mc
Comments
Post a Comment