使用oracle数据库12 我有这个数据
C:\full\path\to\viewers\apps\maxq\
我想要的是在X2中找到共同的值,然后返回X1 在这种情况下,我想得到42974和42975,因为他们共享相同的X2
我使用此查询,但它不会返回任何内容
X1 X2 X3
42858 RRR08401000A 1
42858 RRR06113000A 1
42858 RRR05529000A 1
42858 RRR05529000B 1
42858 RRR49110RRRF 1
42858 34100000000A 1
42974 XYZ39916000A 1
42974 XYZ00949000B 2
42974 XYZ04888000A 1
42974 XYZ39915000B 1
42974 XYZ38535000A 1
42974 XYZ42350000A 1
42974 XYZ39746X0AA 1
42974 XYZ39743X0AA 1
42974 XYZ39923000A 1
42974 XYZ39745X0AA 1
42974 XYZ39744000A 1
42974 XYZ03058X0AB 1
42974 XYZ39759000A 1
42974 XYZ39760000B 1
42974 XYZ39762000A 1
42974 XYZ39748000A 1
42974 XYZ39749000B 1
42974 XYZ39747000A 1
42974 XYZ39742X0AA 1
42974 XYZ47965000B 1
42974 IUYI0902000E 1
42974 IUYI0897000F 1
42974 IUYI0909000F 1
42975 XYZ39916000A 1
42975 XYZ00949000B 2
42975 XYZ04888000A 1
42975 XYZ39915000B 1
42975 XYZ38535000A 1
42975 XYZ42350000A 1
42975 XYZ39746X0AA 1
42975 XYZ39743X0AA 1
42975 XYZ39923000A 1
42975 XYZ39745X0AA 1
42975 XYZ39744000A 1
42975 XYZ03058X0AB 1
42975 XYZ39759000A 1
42975 XYZ39760000B 1
42975 XYZ39762000A 1
42975 XYZ39748000A 1
42975 XYZ39749000B 1
42975 XYZ39747000A 1
42975 XYZ39742X0AA 1
42975 XYZ47965000B 1
42975 IUYI0902000E 1
42975 IUYI0897000F 1
42975 IUYI0909000F 1
但如果我从数据中删除了42858,则返回42974和42975
请告知
感谢, 旅行男人
SELECT
TT.X1
FROM TEST12 TT
group by TT.WORK_ORDER
having count(distinct TT.X2) = (select count (distinct tt2.X2) from TEST12 tt2)
答案 0 :(得分:1)
尝试加入:
SELECT DISTINCT T1.X1
FROM TEST12 T1
JOIN TEST12 T2 ON T1.X2 = T2.X2
AND T1.X1 != T2.X2
如果三个或更多行共享DISTINCT
值,则需要X2
。如果最多只有两个行共享X2
值,则DISTINCT
关键字可能会被省略。
答案 1 :(得分:0)
以下是一些选项。 X2元素的数量有限制,因为它们被连接成一个字符串。
select count (*) as x2_within_group
,x2_elements
,listagg (x1,',') within group (order by x1) as x1_list
from (select X1
,listagg (x2,',') within group (order by x2) as x2_list
,count (*) as x2_elements
from TEST12
group by X1
)
group by x2_list
,x2_elements
;
X2_WITHIN_GROUP X2_ELEMENTS X1_LIST
---------------- ---------- ---------
2 23 42974,42975
1 6 42858
select dense_rank () over (order by listagg (x2,',') within group (order by x2)) as group_id
,X1
,count (*) as x2_elements
from TEST12
group by X1
;
GROUP_ID X1 X2_ELEMENTS
---------- ---------- ------------
1 42858 6
2 42975 23
2 42974 23