Table: Number
A B
----- --
12 34
22 34
11 35
13 36
Table: Data
B C D
----- ----- --
34 A B
34 C D
35 E F
35 G H
36 I J
36 K L
I want to return value in B when A is a duplicate mapping to B. In the example, 34 is selected(in practice, it will return over 100+ duplicate values).
After getting 34, I want to return all rows in 'Data' table where B=34
B C D
----- ----- -----
34 A B
34 C D
My Try:
With Number as (
select B,count(B) as count
from Number
where 1=1
group by B
)
, Number2 As (
select B
from Number
where count>1
, Data As (
select * from Data where B in (select * from Number2)
)
But when I run the script, it runs extremely slow. I am wondering if there is any better way to make it faster