join - SQL not including duplicate values on a combination of 2 columns -
i working on exercise 16 sql-ex.ru. problem asks following:
find pairs of pc models having identical speeds , ram. result, each resulting pair shown once, i.e. (i, j) not (j, i). result set: model higher number, model lower number, speed, , ram.
the database schema :
product(maker, model, type) pc(code, model, speed, ram, hd, cd, price) laptop(code, model, speed, ram, hd, screen, price) printer(code, model, color, type, price)
i wrote following query:
select a.model, b.model, a.speed, a.ram pc join pc b on (a.model<>b.model) a.speed=b.speed , a.ram=b.ram
but displays duplicates of i,j j,i. here output:
model model speed ram 1121 1233 750 128 1232 1233 500 64 1232 1260 500 32 1233 1121 750 128 1233 1232 500 64 1260 1232 500 32
as can see, values of i,j flipped , counted distinct values. there easy way rid of duplicates this? kind of lost on part.
i think "model higher number, model lower number" in problem statement clue need have a.model > b.model
condition somewhere. join's on
condition sounds fine candidate:
select a.model, b.model, a.speed, a.ram pc join pc b on (a.model > b.model) -- <<<=== here a.speed=b.speed , a.ram=b.ram
the <>
symmetrical; >
not. switching >
ensures if {i, j}
in, {j, i}
out sure.
Comments
Post a Comment