Find out with 1006 4 5 6 7 11 repeat Numbers 3 and above, there are 1003, and 1005 two records, how shall we write the SQL, thank you!
CodePudding user response:
With a as (select 1001 id, 1 num1, 3 num2, 4 num3, num4 6, 10 num5 from dual union all Select 1002,8,4,5,2,10 from dual union all Select 1003,1,5,6,7,11 from dual union all Select 1004,3,9,7,6,2 from dual union all Select 1005,5,6,7,8,9 from dual union all Select 1006,4,5,6,7,11 from dual) , b, as ( Select id, num1 from a union all Select id, num2 from a union all Select id, num3 from a union all Select id, num4 from a union all Select id, num5 from a ) Select id, count (1) the from (the select distinct t1. Id, t1. Num1 The from t1, b b t2, t3 b Where a t1. Num1=t2. Num1 And t2. Num1=t3. Num1 And t1. Id & lt;> T2. Id And t2. Id & lt;> T3. Id And t1. Id & lt;> T3. Id) Group by id having count (1) & gt;=3 ;
CodePudding user response:
With a as (select 1001 id, 1 num1, 3 num2, 4 num3, num4 6, 10 num5 from dual union all Select 1002,8,4,5,2,10 from dual union all Select 1003,1,5,6,7,11 from dual union all Select 1004,3,9,7,6,2 from dual union all Select 1005,5,6,7,8,9 from dual union all Select 1006,4,5,6,7,11 from dual) , b, as ( Select id, num1 from a union all Select id, num2 from a union all Select id, num3 from a union all Select id, num4 from a union all Select id, num5 from a ) The select t2. Id, count (distinct t2. Num1) The from b t1, t2 b Where a t1. Num1=t2. Num1 And t1. Id & lt;> T2. Id And t1. Id=1006 Group by t2. Id having count (distinct t2. Num1) & gt;=3 ;