1

My SQL is rusty, so i need help, and i hope i don't get confusing with my explanation.

I need a statement to find all duplicate values of Field A, if at least one of those rows has a constant value on Field B

Field A | Field B
dog      | Canines
dog      | NULL
dog      | NULL

Because Field B is Canines, i want all rows that duplicate the Field A value. I need a select statement, but my intention is to change a value on Field C for duplicates, but i can probably figure that out afterwards.

2
  • 3
    What would the output be? Just one row with dog | canines? Commented May 24, 2013 at 15:48
  • I am pretty much just looking for how to select them, so output isn't that important, as long as i have the data. I will be updating the table to change Field C from 0 to 1, for all duplicate values of fields A where one of them is the same as Field B, but does not contain the value of Field B Commented May 24, 2013 at 15:51

4 Answers 4

2

It seems that you want all rows, where the FieldB value is not uniformly NULL.

This gives you the list of FieldAs with that characteristic:

select FieldA
from t
group by FieldA
having count(FieldB) > 0;

If you actually want the rows with the NULL values, then:

select FieldA, FieldB
from (select t.*, count(FieldB) over (partition by FieldA) as cnt
      from t
     ) t
where cnt = 0;

To get all FieldA where one of them is canines:

select distinct FieldA
from t
where FieldB = 'canine';

To get all the rows, use this in a where clause:

select *
from t
where t.FieldA in (select distinct FieldA
                   from t
                   where FieldB = 'canine'
                  );
Sign up to request clarification or add additional context in comments.

2 Comments

I need to select all rows where there is a duplicate of Field A, and at least one of the Field B's equal 'Canines'. There are loads of other Field B values that i want to ignore.. I only want duplicates that have a specific Field B value.
Something like: SELECT FieldA FROM t WHERE FieldB = 'Canines' AND (Find all dups of FieldA)
2

Your help allowed me to find it.. Thank you all. What i was looking for was:

SELECT FieldA
FROM t
WHERE FieldB = 'Canines'
AND FieldA 
IN
(
    SELECT FieldA
    FROM t
    GROUP BY FieldA
    HAVING COUNT(FieldA) > 1
)

Thank you! Thank you!

Comments

2

To get all rows where count(fielda) > 1 and one of fieldb has the value canine

select t1.fielda
from mytab t1
join (select fielda, count(*) as dups
      from mytab
      group by fielda) t2 on t2.fielda = t1.fielda
where t1.fieldb = 'canine'
and t2.dups > 1

SQLFiddle

Comments

1

If you want every row where there are duplicates, and where at least one item in Field B is not null, this will pull that:

SELECT *
FROM Table
WHERE FieldA IN (SELECT DISTINCT FieldA
                 FROM Table
                 WHERE FieldB IS NOT NULL)
   AND FieldA IN (SELECT FieldA
                 FROM Table
                 GROUP BY FieldA
                 HAVING COUNT(*)>1)

2 Comments

Not looking for Field B to be NOT NULL. Looking for at least one of the items to have the value of 'Canines' in Field B
I think Goat_CO's query was intended to allow the user to not have to hard code "canine" and automatically pick up "feline" or "ursine" or whatever else may be there. Sort of a bigger picture thing. I might be wrong.

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.