2011-06-09 46 views
0

我有一個包含CustomerIDs和SalespersonIDs的表(order_t),我想要顯示與所有與其關聯的CustomerIDs的SalespersonIDs。SQL獲取非重複值[Oracle SQL * Plus]

我嘗試使用SELECT DISTINCT

SELECT DISTINCT salespersonid, customerid 
FROM order_t; 

但是,這將重複salespersonids。

這裏有一個表樣品

salespersonid customerid 
--------------- --------------- 
15    1 
15    2 
15    3 
16    4 
16    5 
17    6 

而且我想這個數據集作爲結果

salespersonid customerid 
--------------- --------------- 
15    1 
       2 
       3 
16    4 
       5 
17    6 
18    4 
       5 
       9 

這完全

BREAK ON sid 
SELECT DISTINCT salespersonid sid, customerid cid 
FROM ORDER_T 
ORDER BY salespersonid; 
+1

是的,所有這一切。 – StudentJ 2011-06-09 17:11:54

回答

2

在SQL * Plus,你會做以下幾點:

SQL> BREAK ON sid 

SQL> SELECT salespersonid sid, customerid cid 
     FROM order_t 
     ORDER BY salespersonid; 

應該給你的輸出看起來像這樣(未經):

SID  CID 
----- ------ 
    1  1 
      2 
      3 
    2  1 
      7 
    3  1 
... 

編輯:

如果您需要每個salespersonid的單行,請參閱this Stackoverflow question關於如何執行此操作。這不是微不足道的事情。

0

工作SELECT DISTINCT將返回非salespersonid和customerid的重複組合。

換句話說,它可以返回相同的salespersonid,但具有不同的customerid。

例如 1,1
1,2
1,3 ...

,但它不會返回1,1 | 1,2 | 1,3再次