表数据集:
SSN ID F_NAME L_NAME
000000000 1 JANE DOE
123456789 2 PETE PETER
123456789 3 JOHN SHAW
234567890 4 ALAN DAVID
234567890 5 MAY APRI
345677891 6 JUNE ALYSSA
456789123 7 LISA LINH
567891234 8 HAL LEY
567891234 9 DEREK LI
567891234 10 JIM JAMES
从上面的数据集中,我的目的是识别具有相同SSN的ID /人口统计数据,并水平列出所有名称和结果,如下所示。我们还对与每个SSN关联的唯一ID计数感兴趣。
SSN COUNT ID F_NAME L_NAME ID_2 F_NAME_2 L_NAME_2 ID_3 F_NAME_3 L_NAME_3
123456789 2 2 PETE PETER 3 JOHN SHAW
234567889 2 4 ALAN DAVID 5 MAY APRI
567891234 3 8 HAL LEY 9 DEREK LI 10 JIM JAMES
帮助?
答案 0 :(得分:1)
我会以下列方式使用listagg,您也可以添加唯一ID:
create table demographics(
ssn number(9),
id number(12),
f_name varchar2(50),
l_name varchar2(50));
insert into demographics values(000000000, 1, 'JANE', 'DOE');
insert into demographics values(123456789, 2, 'PETE', 'PETER');
insert into demographics values(123456789, 3, 'JOHN', 'SHAW');
insert into demographics values(234567890, 4, 'ALAN', 'DAVID');
insert into demographics values(234567890, 5, 'MAY', 'APRI');
insert into demographics values(345677891, 6, 'JUNE', 'ALYSSA');
insert into demographics values(456789123, 7, 'LISA', 'LINH');
insert into demographics values(567891234, 8, 'HAL', 'LEY');
insert into demographics values(567891234, 9, 'DEREK', 'LI');
insert into demographics values(567891234, 10, 'JIM', 'JAMES');
SELECT ssn, count(ssn) as count, LISTAGG(f_name || ' ' || l_name, ', ') WITHIN GROUP (ORDER BY f_name || ' ' || l_name) "Names"
FROM demographics
group by ssn;
SSN COUNT Names
0 1 JANE DOE
123456789 2 JOHN SHAW, PETE PETER
234567890 2 ALAN DAVID, MAY APRI
345677891 1 JUNE ALYSSA
456789123 1 LISA LINH
567891234 3 DEREK LI, HAL LEY, JIM JAMES
答案 1 :(得分:1)
根据@ lad2025的建议
查看有关pivot here的更多信息根据您可以使用的格式,我将使用的快速解决方案是SQL (Oracle)中的listagg
函数,类似于MySQL group_concat
,例如< / p>
SELECT SSN,
count(*),
LISTAGG(ID, ', ') WITHIN GROUP (ORDER BY ID),
LISTAGG(F_NAME, ', ') WITHIN GROUP (ORDER BY F_NAME)
FROM table GROUP BY SSN HAVING count(*) > 1;
会导致
123456789 2 2,3 PETE, JOHN
234567889 2 4,5 ALAN, MAY
567891234 3 8,9,10 HAL, DEREK, JIM
或者,您可以识别具有重复SSN的所有行 - 这将避免使用pivot
并将结果保持为相同的关系格式
SELECT *
FROM table
WHERE SSN in (SELECT SSN from table group by SSN having count(*) > 1);
将返回包含重复SSN的所有行:
SSN ID F_NAME L_NAME
123456789 2 PETE PETER
123456789 3 JOHN SHAW
234567890 4 ALAN DAVID
234567890 5 MAY APRI
567891234 8 HAL LEY
567891234 9 DEREK LI
567891234 10 JIM JAMES