以下是表(客戶),並記錄如下:我想在Oracle表中選擇非重複的記錄(只選擇重複記錄)
CUSTID CUSTNAME
100 ashwin
100 ashwin
200 adithyan
100 ashwin
300 aravind
300 aravind
300 aravind
我想要的結果是這樣(非重複行):
CUSTID CUSTNAME
100 ashwin
100 ashwin
300 aravind
300 aravind
以下是表(客戶),並記錄如下:我想在Oracle表中選擇非重複的記錄(只選擇重複記錄)
CUSTID CUSTNAME
100 ashwin
100 ashwin
200 adithyan
100 ashwin
300 aravind
300 aravind
300 aravind
我想要的結果是這樣(非重複行):
CUSTID CUSTNAME
100 ashwin
100 ashwin
300 aravind
300 aravind
試試這個:
select custid, custname
from customer a where
exists (
select CUSTID, CUSTNAME, count(*)
from customer b
where a.custid = b.custid and a.custname = b.custname
group by CUSTID, CUSTNAME
having count(*) > 1)
它給出了具有重複項的所有行的結果。它選擇所有3行custid = 100。但我只想要非獨特的記錄,只有2行custid = 100. – Ashwinkumar 2015-03-25 08:16:59
我想要的結果是這樣的(非重複行):
CUSTID CUSTNAME 100 ashwin 100 ashwin 300 aravind 300 aravind
如果我理解正確的話,你要重複行的其餘部分。您可以使用分析ROW_NUMBER。
測試用例:
SQL> CREATE TABLE t
2 (CUSTID int, CUSTNAME varchar2(8));
Table created.
SQL>
SQL> INSERT ALL
2 INTO t (CUSTID, CUSTNAME)
3 VALUES (100, 'ashwin')
4 INTO t (CUSTID, CUSTNAME)
5 VALUES (100, 'ashwin')
6 INTO t (CUSTID, CUSTNAME)
7 VALUES (200, 'adithyan')
8 INTO t (CUSTID, CUSTNAME)
9 VALUES (100, 'ashwin')
10 INTO t (CUSTID, CUSTNAME)
11 VALUES (300, 'aravind')
12 INTO t (CUSTID, CUSTNAME)
13 VALUES (300, 'aravind')
14 INTO t (CUSTID, CUSTNAME)
15 VALUES (300, 'aravind')
16 SELECT * FROM dual;
7 rows created.
SQL>
SQL> COMMIT;
Commit complete.
SQL>
查詢使用ROW_NUMBER
SQL> select custid, custname
2 from (
3 SELECT custid, custname,
4 row_number() over (partition by custid order by custid) as rn
5 from t
6 )
7 WHERE rn > 1
8 order by custid;
CUSTID CUSTNAME
---------- --------
100 ashwin
100 ashwin
300 aravind
300 aravind
SQL>
使你所需的輸出。
是的,它的工作原理非常好,謝謝你的解決方案:-) – Ashwinkumar 2015-03-25 08:27:14
@Ashwinkumar不客氣!請標記爲已回答,這也會幫助其他人, – 2015-03-25 08:28:36
想選擇非截然不同的行 – Ashwinkumar 2015-03-25 08:11:30
編輯,而不是評論您自己的問題 – Alexander 2015-03-25 08:17:01
@Ashwinkumar如果答案已解決您的問題,請將答案標記爲已回答。閱讀[當某人回答我的問題時該怎麼辦?](http://stackoverflow.com/help/someone-answers) – 2015-03-25 09:00:41