0

I checked https://stackoverflow.com/a/8149330/11343720 but, I want change the condition with OR instead of AND.
I need get dupliate data with duplicate name or duplicate city.
How I can do this?

select s.id, t.* 
from [stuff] s
join (
    select name, city, count(*) as qty
    from [stuff]
    group by name, city
    having count(*) > 1
) t on s.name = t.name OR s.city = t.city

So I want to do something like this SQL code below:

select s.id, s.name,s.city 
from stuff s
group by s.name having count(where city OR name are identical) > 1
7
  • What is wrong with AND? Commented Jun 22, 2021 at 0:14
  • @MERN Why is that "your requirement"? Who is giving you your "requirements"? (Sounds to me like you have a micromanager...) Are you perhaps confusing the English-language definition of "or" and "and" as opposed to the formal-logic definition of "or" and "and" which are actually very different to the English-language definitions? Commented Jun 22, 2021 at 0:15
  • 1
    We need you to post concrete examples of the "duplicate data" you expect to see - because an overly-inclusive join criteria will always result in duplicate rows in the output, but that doesn't mean there's duplicate source data. Commented Jun 22, 2021 at 0:18
  • Might want to check out stackoverflow.com/questions/5901791/… Commented Jun 22, 2021 at 0:19
  • @Strawberry, Ok, I want to close this question, please help me Commented Jun 22, 2021 at 8:19

3 Answers 3

1

Maybe I misunderstood you initially. You want either name or city duplications, right?

select s.* 
from [stuff] s
where name in 
(
    select name
    from [stuff]
    group by name
    having count(*) > 1
) OR
city in (select city
    from [stuff]
    group by city
    having count(*) > 1) 
Sign up to request clarification or add additional context in comments.

Comments

1

I need get duplicate data with duplicate name or duplicate city.

I would suggest window functions:

select s.*
from (select s.*,
             count(*) over (partition by name) as name_cnt,
             count(*) over (partition by city) as city_cnt
      from stuff s
     ) s
where name_cnt > 1 or city_cnt > 1;

Comments

0

I tried to keep this solution similar to the original solution you linked.

select s.id, 
       t.* 
from [stuff] s
join (
    select t.name, 
           t.city
    from [stuff] t
    join [stuff] t2
    on t.name = t2.name
        or t.city = t2.city
    group by t.name, t.city
    having count(*) > 1
) t 
on coalesce(s.name,'') = coalesce(t.name,'')
and coalesce(s.city,'') = coalesce(t.city,'')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.