i have query
select distinct re1.name name1,re2.name name2 rating r1,rating r2,reviewer re1,reviewer re2 r1.rid > r2.rid , r1.mid = r2.mid , re1.rid = r1.rid , re2.rid = r2.rid;
the output is
name1 name2 chris brittany ashley chris james elizabeth
i want return table in such way rows in alphabetical order, i.e. output should be:
name1 name2 brittany chris ashley chris elizabeth james
you can use least()
, greatest()
functions:
select distinct least(re1.name, re2.name) name1, greatest(re1.name, re2.name) name2 rating r1,rating r2,reviewer re1,reviewer re2 r1.rid > r2.rid , r1.mid = r2.mid , re1.rid = r1.rid , re2.rid = r2.rid;
you should learn proper join
syntax:
select distinct least(re1.name, re2.name) name1, greatest(re1.name, re2.name) name2 rating r1 join rating r2 on r1.rid > r2.rid , r1.mid = r2.mid , reviewer re1 on re1.rid = r1.rid join reviewer re2 on re2.rid = r2.rid ;
edit:
if database doesn't support these 2 functions, can same things case
statement:
select distinct (case when re1.name < re2.name re1.name else re2.name end) name1, (case when re1.name < re2.name re2.name else re1.name end) name2 . . .
as issue null
s. original data has names populated , joins inner joins, null
did not seem issue.
Comments
Post a Comment