I want to select all rows that have a value that already exists in the table. I did not find a better solution than
select *
from provisioning_requests tt
where code in (select code
from provisioning_requests tt2
where tt2.id <> tt.id)
This seems a bit naive. Does anybody have a better solution?
select *
from provisioning_requests t1
join (select code from provisioning_requests group by code having count(*)>1) t2
ON t1.code = t2.code
OR
select *
from provisioning_requests
WHERE code in (select code from provisioning_requests group by code having count(*)>1)
An Auto join do the job
select tt.*
from provisioning_requests tt
INNER JOIN provisioning_requests tt2
ON tt.code = tt2.code
AND tt2.id <> tt.id
select t.*
from(
select *, count(1) over(partition by code) as cnt
from test
) as t
where t.cnt > 1
You can use operator exists
, it produces better performance:
select *
from provisioning_requests tt
where exists
(
select 1
from provisioning_requests tt2
where tt2.id <> tt.id and tt2.code = tt.code
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With