oracle 中的 Pivot 没有给出预期的结果
Pivot in oracle is not giving the expected result
我有一个 table 结果如下
我有一个 table 如下所示:
PE_ID
Phone_Num
Email
740
NULL
test@test.com
740
124567890
NULL
我正在尝试得到如下结果
PE_ID
Phone_Num
Email
740
124567890
test@test.com
我曾尝试使用以下 PIVOT
查询,但它无法正常工作
with rws as (
select 740 type, NULL Phone_Num, 'test@test.com' email from dual union all
select 740 type, '711692162' Phone_Num, NULL email from dual
), rns as (
select row_number () over ( order by type ) rn,
r.*
from rws r
)
select * from rns
pivot (
min ( type ) Pe_ID, min ( Phone_Num ) Phone_Num, min ( email ) email
for rn in (1)
);
给出的结果如下
PE_ID_1
Phone_Num_1
Email_1
740
NULL
test@test.com
请任何人为我指出预期结果的正确方向
您似乎想要按类型分组并查找最少的 phone 号码和电子邮件,并且 return 仅查找最少的类型。您不需要 PIVOT
,只需聚合 return 第一行:
SELECT type,
MIN( phone_num ) AS phone_num,
MIN( email ) email
FROM table_name
GROUP BY type
ORDER BY type
FETCH FIRST ROW ONLY
其中,对于示例数据:
CREATE TABLE table_name ( type, phone_num, email ) AS
select 740, NULL, 'test@test.com' from dual union all
select 740, '711692162', NULL from dual
输出:
TYPE
PHONE_NUM
EMAIL
740
711692162
test@test.com
db<>fiddle here
您根本不需要 PIVOT
。您可以使用 FIRST_VALUE
分析功能获取第一个 phone 号码和电子邮件。
WITH
rws
AS
(SELECT 740 TYPE, NULL Phone_Num, 'test@test.com' email FROM DUAL
UNION ALL
SELECT 740 TYPE, '711692162' Phone_Num, NULL email FROM DUAL)
SELECT DISTINCT
TYPE,
FIRST_VALUE (phone_num IGNORE NULLS) OVER (ORDER BY TYPE) AS phone_num,
FIRST_VALUE (email IGNORE NULLS) OVER (ORDER BY TYPE) AS email
FROM rws;
TYPE PHONE_NUM EMAIL
_______ ____________ ________________
740 711692162 test@test.com
SELECT peid, listagg(phone_num) phone_num ,
listagg(email) email FROM table1 GROUP BY peid;
我有一个 table 结果如下
我有一个 table 如下所示:
PE_ID | Phone_Num | |
---|---|---|
740 | NULL | test@test.com |
740 | 124567890 | NULL |
我正在尝试得到如下结果
PE_ID | Phone_Num | |
---|---|---|
740 | 124567890 | test@test.com |
我曾尝试使用以下 PIVOT
查询,但它无法正常工作
with rws as (
select 740 type, NULL Phone_Num, 'test@test.com' email from dual union all
select 740 type, '711692162' Phone_Num, NULL email from dual
), rns as (
select row_number () over ( order by type ) rn,
r.*
from rws r
)
select * from rns
pivot (
min ( type ) Pe_ID, min ( Phone_Num ) Phone_Num, min ( email ) email
for rn in (1)
);
给出的结果如下
PE_ID_1 | Phone_Num_1 | Email_1 |
---|---|---|
740 | NULL | test@test.com |
请任何人为我指出预期结果的正确方向
您似乎想要按类型分组并查找最少的 phone 号码和电子邮件,并且 return 仅查找最少的类型。您不需要 PIVOT
,只需聚合 return 第一行:
SELECT type,
MIN( phone_num ) AS phone_num,
MIN( email ) email
FROM table_name
GROUP BY type
ORDER BY type
FETCH FIRST ROW ONLY
其中,对于示例数据:
CREATE TABLE table_name ( type, phone_num, email ) AS
select 740, NULL, 'test@test.com' from dual union all
select 740, '711692162', NULL from dual
输出:
TYPE PHONE_NUM 740 711692162 test@test.com
db<>fiddle here
您根本不需要 PIVOT
。您可以使用 FIRST_VALUE
分析功能获取第一个 phone 号码和电子邮件。
WITH
rws
AS
(SELECT 740 TYPE, NULL Phone_Num, 'test@test.com' email FROM DUAL
UNION ALL
SELECT 740 TYPE, '711692162' Phone_Num, NULL email FROM DUAL)
SELECT DISTINCT
TYPE,
FIRST_VALUE (phone_num IGNORE NULLS) OVER (ORDER BY TYPE) AS phone_num,
FIRST_VALUE (email IGNORE NULLS) OVER (ORDER BY TYPE) AS email
FROM rws;
TYPE PHONE_NUM EMAIL
_______ ____________ ________________
740 711692162 test@test.com
SELECT peid, listagg(phone_num) phone_num ,
listagg(email) email FROM table1 GROUP BY peid;