SQL exclude duplicated pairs of columns from a two-column select -


i don't know how describe problem , haven't found answer. have 2 tables, rating , reviewer. want every pair of reviewers names if rated same movie. have sql query:

select distinct re1.name, re2.name  reviewer re1, reviewer re2, rating ra1, rating ra2 re1.rid=ra1.rid , re2.rid=ra2.rid , ra1.mid=ra2.mid , re1.rid!=re2.rid; 

this tables structure: rating (rid, mid, stars, ratingdate), reviewer(rid, name)

this want get:

name                       name               daniel lewis               elizabeth thomas  elizabeth thomas           james cameron  ashley white               chris jackson mike anderson              sarah martinez brittany harris            chris jackson  

this get:

name                           name                          ------------------------------ ------------------------------ daniel lewis                   elizabeth thomas                elizabeth thomas               james cameron                   chris jackson                  brittany harris                 chris jackson                  ashley white                    ashley white                   chris jackson                   james cameron                  elizabeth thomas                mike anderson                  sarah martinez                  sarah martinez                 mike anderson                   elizabeth thomas               daniel lewis                    brittany harris                chris jackson    

how remove duplicate rows name1 , name2 have been selected name2, name1 ? hope clear enough. thanks.

its subtle change (> vs !=), want use diagonal elimination approach, whereby exclude reviewer rid lower current one:

select distinct re1.name, re2.name  reviewer re1   inner join rating ra1     on re1.rid=ra1.rid cross join   reviewer re2   inner join rating ra2     on re2.rid=ra2.rid ra1.mid=ra2.mid ,  re1.rid > re2.rid; 

this way, don't 'double count' pairs of reviewers have match on opposite / symmetric side of diagonal.

i've taken liberty of moving join conditions joins, rather in where clause.

sqlfiddle here


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -