2011-06-09 112 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再次