lundi 8 juin 2015

SQL: count the number of values two distinct entries have

Given the following SQLite table:

Name  | Lived_in
----------------
John  | Canada
John  | UK
James | Canada
James | UK
Mary  | UK
Mike  | USA

Is there any way of listing the pairs formed by the distinct values in col_1 (Names), alongside the number of values they share in col_2 (Lived_in), and sorted by the later?

In my example, this would yield:

 Node A |  Node B | Shared values
-------------------------------
 John   |  James  |  2
 John   |  Mary   |  1
 James  |  Mary   |  1

Note: if that can't be done, I'll accept an answer that returns all pairs that have X relationships in common, where I have to specify what X is for each request.

Aucun commentaire:

Enregistrer un commentaire