0

I've this table with the following data:

CaseID|ProductNr|CodeNr|Test
    1 |1234     |444   |No
    1 |1235     |444   |No
    1 |1236     |444   |No
    1 |1237     |666   |No
    1 |1238     |777   |No
    2 |1244     |555   |No
    2 |1245     |555   |No

I want a query that find all the "ProductNr" that has the same "CodeNr" within the same "CaseID".

E.g. if .. Where CaseId = 1 the result should be as below:

CaseID|ProductNr|CodeNr|Test
    1 |1234     |444   |No
    1 |1235     |444   |No
    1 |1236     |444   |No

I'm using sql server

It should be possible to change the CaseId value.. The CaseId value should be a parameter.. because the query need to be a part af stored procedure..

3
  • Either EXISTS, or self-join. Commented Feb 24, 2016 at 15:44
  • according to your criteria, shouldn't the result be every row with CaseId = 1?, or is it another requirement that there must be duplicates? Commented Feb 24, 2016 at 15:46
  • Possible duplicate of How do I find duplicates across multiple columns? Commented Feb 24, 2016 at 15:52

2 Answers 2

3

The following query shows you those rows of YourTable whose CaseID is equals to the given parameter and that there is at least one row on the same table with the same CaseID, the same CodeNr, but different ProductNr. This is what you are looking for?

SELECT *
FROM YourTable t
WHERE EXISTS (
   SELECT *
   FROM YourTable i
   WHERE t.CaseID = i.CaseID AND t.ProductNr <> i.ProductNr AND t.CodeNr = i.CodeNr
) AND t.CaseID = @CaseID
Sign up to request clarification or add additional context in comments.

4 Comments

Hi thanks.. but it should be possible to change the CaseId value.. The CaseId value should be a parameter.. because the query need to be a part af stored procedure..
@Balatharan Edited to add @CaseID parameter
Hi Jesus, your query give me the desired result thanks a lot.. just a another question does the EXISTS give any problem, someone told me to not use EXISTS, i don't know quite way?
No problem with EXISTS at all. But if you want your query to be fast, add an index to the table on the columns: CaseID and CodeNr. CREATE INDEX IX_YourTable_CaseID_CodeNr ON YourTable(CaseID, CodeNr) INCLUDE (ProductNr)
-1

You can use:

SELECT CaseID, ProductNr, CodeNr, Test
FROM (
  SELECT CaseID, ProductNr, CodeNr, Test,
         COUNT(CodeNr) OVER (PARTITION BY CodeNr) AS cnt
  FROM mytable
  WHERE CaseID = 1) AS t
WHERE t.cnt > 1

COUNT window function is applied on CodeNr partitions. Any record that belongs to a partition with a population greater than 1 will be returned by this query.

Note: You can additionally use RANK in case of more than one groups of duplicate records, so as to discern between records that belong to separate CodeNr groups.

Note2: This query works as long as each CodeNr partition contains only unique ProductNr values.

1 Comment

My initial attempt was at the wrong path, had to completely rewrite my answer. Apologies.

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.