Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
113 views
in Technique[技术] by (71.8m points)

sql - Compare data in same table oracle

I need to compare data between two different ids in the same table.

Eg) Id 123 can have items a,b,c
Id 234 can have items x,a,b
And id 789 can have items a,b,c.

My sql should be able to say that Id 123 and Id 789 are the same based on the items column , or say that Id 123 or id 234 are different based on item column.

I guess we can do this with self join in oracle , but I am not sure how .

Correction— Items a, b , c are in 3 different records for I’d 123 or for I’d 789.

question from:https://stackoverflow.com/questions/65941434/compare-data-in-same-table-oracle

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use cte and join each record with all other records for comparision as follows:

With cte as
(Select id, listagg(item,',') within group (order by item) as items
   From your_table t
  Group by id)
Select t1.id, t2.id,
       Case when t1.items = t2.items then 'match' else 'do not match' end as res_
 From cte t1 join cte t2 on t1.id < t2.id 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...