0

Let me first give some example table table which will make my question easier to understand.

Column1      Column2      Column3
Valuea       Value123     Value456
Valueax      Value123     Value456
Valueb       Value123     Value456
Valueb       Value123     Value456

select   Column1,
     Column2,
         Column3,
         count(*)
from     ColumnName
group by 
     Column1,
     Column2,
     Column3,
having   count(Count2+Count3) > 1 

I want to return 'column1', 'column2', 'column3' where column 2and3 will contain more than more duplicates but 'column1' must be different. So at present the above code would return:

Valueb       Value123     Value456 2

but i want it to return

Valuea       Value123     Value456
Valueax      Value123     Value456 

where column1 has different values but column2 and column3 values are the same.

2 Answers 2

3

You can't group by concatenating. This will treat 'xxx' + 'yyy' the same as 'x' + 'xxyyy'. How about getting

;WITH x AS 
(
   SELECT Column2, Column3 
   FROM dbo.table GROUP BY Column2, Column3
   HAVING COUNT(*) > 1
)
SELECT t.Column1, t.Column2, t.Column3
FROM x INNER JOIN dbo.table AS t
ON x.Column2 = t.Column2
AND x.Column3 = t.Column3
GROUP BY t.Column1, t.Column2, t.Column3
HAVING COUNT(*) = 1;

This assumes that none of these columns are NULLable.

Sign up to request clarification or add additional context in comments.

1 Comment

Note that if you have three rows and some are duplicates (e.g. Valuea, Valueax and Valueax) the latter two won't be reported... if these are potential cases you'll need to supply more sample data and how you want to handle those cases.
0

If I understand your question correctly,

You want to have unique row record for your table. Instead of using Group By who Aaron mentioned you can't use in concatenating.

However , you can use Over to do that.

UPDATE : you need to consider what are you need to group together as Aaron mentioned "x" + "yy" is identical with "xy" + "y"

With Data AS
(
SELECT ROW_NUMBER() Over 
     (Partition By "Key" Order by "Key") as R,
     Column1, Column2, Column3
)
SELECT * FROM Data 
WHERE R = 1 

3 Comments

Partition By Column1+Column2+Column3? That is rather dangerous. 'x' + 'yy' + 'z' will be treated the same as 'xy' + 'y' + 'z' but I don't consider those rows identical. In any case even if you ignore that I don't think this gives the desired result - why WHERE R = 1?
I didn't notice that after read your post. I'll edit the answer.
Still don't see how this could possibly derive the desired result.

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.