我有一个包含多个列的数据集,看起来与此类似:
ID1 ID2 ID3 ID4
Blue Grey Fuchsia Green
Black Blue Orange Blue
Green Green Yellow Pink
Pink Yellow NA Orange
我想要做的是计算每个值在四列中重复的次数。例如,这就是我想从上面回来的内容:
ID Replicates
Blue 3
Black 1
Green 3
Pink 2
Grey 1
Yellow 2
Fuchsia 1
Orange 2
我还希望能够在频率> 2时询问数据集中存在哪个ID值。所以预期的结果将是:绿色和蓝色。
有关如何在Oracle中执行此操作的任何想法?谢谢!
答案 0 :(得分:3)
select c, count(*)
from
(
select ID1 as c from tablename
union all
select ID2 as c from tablename
union all
select ID3 as c from tablename
union all
select ID4 as c from tablename
)
group by c
在末尾添加HAVING count(*) > 2
以仅获得绿色和蓝色。
答案 1 :(得分:1)
SELECT ID, COUNT(*) FROM(
SELECT ID1 ID FROM TBL UNION ALL
SELECT ID2 ID FROM TBL UNION ALL
SELECT ID3 ID FROM TBL UNION ALL
SELECT ID4 ID FROM TBL ) Q
GROUP BY Q.ID;
答案 2 :(得分:1)
select id, sum(id_cnt)
(select ID1 as ID,count(*) as id_cnt from tableA group by ID1
union all
select ID2,count(*)from tableA group by ID2
union all
select ID3,count(*)from tableA group by ID3
union all
select ID4,count(*)from tableA group by ID4)
group by id
Outpout将类似于
ID Replicates
Blue 3
Black 1
Green 3
Pink 2
Grey 1
Yellow 2
Fuchsia 1
Orange 2
答案 3 :(得分:1)
以下是使用Oracle unpivot
关键字的另一个非常简单的选项:
select id, count(*) as replicates
from tbl
unpivot
(
id for original_column_name in (id1, id2, id3, id4)
)
group by id
...当然,您可以随意添加having
和/或order by
条款。