2013-04-08 38 views
1

我的工作鍛鍊16從sql-ex.ru的組合重複值。這個問題問以下內容:SQL不包括2列

Find the pairs of PC models having identical speeds and RAM. 
As a result, each resulting pair is shown only once, i.e. (i, j) but not (j, i). 
Result set: model with higher number, model with lower number, speed, and RAM. 

數據庫模式是:

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) 

我寫了下面的查詢:

SELECT A.model, B.model, A.speed, A.ram 
FROM PC A 
JOIN PC B ON (A.model<>B.model) 
WHERE A.speed=B.speed 
AND A.ram=B.ram 

但這顯示我的副本,J爲J,I 。這是我的輸出:

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 

正如您所看到的,i,j的值被翻轉並計數爲不同的值。有沒有簡單的方法來擺脫這樣的重複?我有點失落。

回答

0

我覺得「模型具有較高的數量,以較低的數字模型」中的問題說明的是,你需要有一個A.model > B.model條件地方的線索。加入的ON條件聽起來像一個精緻的候選人:

SELECT A.model, B.model, A.speed, A.ram 
FROM PC A 
JOIN PC B ON (A.model > B.model) -- <<<=== Here 
WHERE A.speed=B.speed 
AND A.ram=B.ram 

<>是對稱的; >不是。切換到>確保如果{i, j}是,那麼{j, i}將缺陣肯定。

+0

這是有道理的。沒有考慮對稱價值。 – Paul 2013-04-08 21:20:17