我有這樣的表:
| ID | 名稱 | 接觸 |
|---|---|---|
| 1 | 一個 | 65489 |
| 1 | 一個 | |
| 1 | 一個 | 45564 |
| 2 | 乙 | |
| 3 | C | 12345 |
| 3 | C | 1234 |
| 4 | D | 32 |
| 4 | D | 324 |
我只想要沒有聯系或聯系長度不是五的用戶。
如果用戶有兩個或多個聯系人,其中一個的長度為五個,其余的不是,則不應將此類用戶包含在表中。
所以,如果客戶至少有一個 5 的聯系長度,我不希望這樣。
所以,我想要這樣的表:
| ID | 名稱 | 接觸 |
|---|---|---|
| 2 | 乙 | |
| 4 | D | 32 |
| 4 | D | 324 |
你能攔住我嗎?
uj5u.com熱心網友回復:
您實際上可以在這里進行范圍檢查:
SELECT id, name, contact
FROM yourTable t1
WHERE NOT EXISTS (
SELECT 1
FROM yourTable t2
WHERE t2.id = t1.id AND TO_NUMBER(t2.contact) BETWEEN 10000 AND 99999
);
請注意,如果contact已經是數字列,則只需洗掉TO_NUMBER上面的呼叫并直接比較。
uj5u.com熱心網友回復:
還有一個選擇:
SQL> with test (id, name, contact) as
2 (select 1, 'a', 65879 from dual union all
3 select 1, 'a', null from dual union all
4 select 1, 'a', 45564 from dual union all
5 select 2, 'b', null from dual union all
6 select 3, 'c', 12345 from dual union all
7 select 3, 'c', 1234 from dual union all
8 select 4, 'd', 32 from dual union all
9 select 4, 'd', 324 from dual
10 )
11 select *
12 from test a
13 where exists (select null
14 from test b
15 where b.id = a.id
16 group by b.id
17 having nvl(max(length(b.contact)), 0) < 5
18 );
ID N CONTACT
---------- - ----------
2 b
4 d 32
4 d 324
SQL>
uj5u.com熱心網友回復:
您可以使用字串函式length并coalesce達到預期的效果。
select id, name, contact from tableA where length(coalesce(contact, '0')) < 5
uj5u.com熱心網友回復:
COUNT分析函式也可用于完成作業。
select id, name, contact
from (
select id, name, contact
, count( decode( length(contact), 5, 1, null ) ) over( partition by id, name ) cnt
from YourTable
)
where cnt = 0
演示
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/407679.html
標籤:
