我有一個包含 4 個欄位的表。first_client_number、first_client_email、second_client_number、second_client_email。就像是:
| first_client_number | first_client_email | second_client_number | second_client_email |
|---|---|---|---|
| 1111 | [email protected] | 2222 | [email protected] |
| 3333 | [email protected] | 3333 | [email protected] |
| 4444 | [email protected] | 4444 | [email protected] |
| 6666 | [email protected] | 7777 | [email protected] |
| 等等 |
在哪里:
- 第一個客戶編號和第二個客戶編號可以不同
- 第一個客戶編號和第二個客戶編號可以相同
我想獲得所有電子郵件的唯一串列,以便我有一列顯示客戶編號,第二列顯示客戶電子郵件地址。我如何展平桌子以獲得我想要的結果?
我在 Oracle v.12 中查詢
謝謝
uj5u.com熱心網友回復:
您描述中的欄位與表格中定義為列標題的欄位不匹配...在問題中:我假設您的表格列標題是正確的
一種方法是使用“UNPIVOT”
檔案鏈接:https : //www.oracle.com/technical-resources/articles/database/sql-11g-pivot.html
但考慮到有限的列,我認為這是最簡單的;拆分資料并使用簡單的聯合
SELECT first_client_number as client_number, first_client_email as Client_email
FROM table
UNION
SELECT second_client_number, second_client_email
FROM Table
筆記:
- 我們無法定義 first 或 second,因此我們必須為列生成別名。我們不需要為第二個查詢設定別名,因為它繼承了第一個查詢的名稱。
- 它假設第一列和第二列的資料型別分別相同。
- 我們不使用,
union all因為我們不想保留重復項,并且union將對結果集執行不同的操作以消除不需要的重復項。
uj5u.com熱心網友回復:
旋轉并選擇不同的:
with
test_data (first_client_number, first_client_email,
second_client_number, second_client_email) as (
select 1111, '[email protected]', 2222, '[email protected]' from dual union all
select 3333, '[email protected]', 3333, '[email protected]' from dual union all
select 4444, '[email protected]', 4444, '[email protected]' from dual union all
select 6666, '[email protected]', 7777, '[email protected]' from dual
)
-- End of test data (not part of the actual query); remove and replace
-- table name with the actual table name
select distinct client_number, client_email
from test_data
unpivot ( (client_number, client_email)
for col in (
(first_client_number, first_client_email),
(second_client_number, second_client_email)
)
)
;
CLIENT_NUMBER CLIENT_EMAIL
------------- ---------------------
3333 na3@na.com
4444 na4@na.com
2222 na2@na.com
6666 na6@na.com
7777 na7@na.com
1111 na1@na.com
uj5u.com熱心網友回復:
由于我對這個問題很感興趣并且出于學習目的,我正在研究您的解決方案并添加了最后 2 行資料。
當我運行您的查詢時,我沒有在輸出中看到這一行 8888, '[email protected]'。我想知道這是否是一個錯誤?
這就是我想出的。你能告訴我我是否正確嗎?我的目標是出于學術目的,并通過 SQL 使自己變得更好。
CREATE TABLE T1(
first_client_number NUMBER,
first_client_email VARCHAR2(25), second_client_number NUMBER, second_client_email VARCHAR2(25)
);
INSERT INTO t1
(first_client_number,
first_client_email, second_client_number,second_client_email)
SELECT 1111, '[email protected]',
2222,
'[email protected]' FROM DUAL UNION ALL
SELECT 3333, '[email protected]', 3333, '[email protected]' FROM DUAL UNION ALL
SELECT 4444, '[email protected]', 4444, '[email protected]' FROM DUAL UNION ALL
SELECT
6666, '[email protected]' ,7777 ,'[email protected]' FROM DUAL UNION ALL
SELECT 8888, '[email protected]', 8888, '[email protected]' FROM DUAL UNION ALL
SELECT 9999, '[email protected]', 9998, '[email protected]'
select distinct
decode(line, 1, first_client_number, second_client_number) client_number,
decode(line, 1, first_client_email, second_client_email) client_email
from t1,
(select 1 line from dual union all select from dual)
order by 1, 2
/
CLIENT_NUMBER CLIENT_EMAIL
------------- -------------------------
1111 na1@na.com
2222 na2@na.com
3333 na3@na.com
4444 na4@na.com
6666 na6@na.com
7777 na7@na.com
8888 na8@na.com
8888na8b@na.com
9998 na9@na.com
9999 na9@na.com
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/385709.html
