SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Finding duplicates + an extra column


Finding duplicates + an extra column

Author
Message
Svante
Svante
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
Points: 3 Visits: 4
Hi, I am stuck with something I think is really simple to solve, but...

In addition to the duplicates, I am also interested to see the value of a column not in the unique list, in my example, col1, col2 and col3
is the key, and col4 not, but I would also see the value of col4 where there are duplicates, anyone there who please could help me?

SELECT col1, col2, col3, col4 FROM table
GROUP BY col1, col2, col3
HAVING COUNT(*) > 1
Mike01
Mike01
SSCrazy
SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)

Group: General Forum Members
Points: 2545 Visits: 1533
Removed

For better, quicker answers, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
bitbucket-25253
bitbucket-25253
SSCoach
SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)

Group: General Forum Members
Points: 15897 Visits: 25280
You might want to look at using a CTE such as:
;with numbered as(SELECT rowno=row_number() over
(Partition by COL1, COL2,COL3 order by COL1),COL1,COL2, COL3,COL4 FROM #TableX)
SELECT COL1, COL2, COL3, COL4 from numbered
WHERE Rowno > 1



If everything seems to be going well, you have obviously overlooked something.

Ron

Please help us, help you -before posting a question please read

Before posting a performance problem please read
Paul White
Paul White
SSC-Dedicated
SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)

Group: General Forum Members
Points: 35906 Visits: 11361
Also:

DECLARE @Table
TABLE (
col1 INTEGER NOT NULL,
col2 INTEGER NOT NULL,
col3 INTEGER NOT NULL,
col4 INTEGER NOT NULL
);

INSERT @Table (col1, col2, col3, col4) VALUES (1, 2, 3, 4);
INSERT @Table (col1, col2, col3, col4) VALUES (1, 2, 3, 5);
INSERT @Table (col1, col2, col3, col4) VALUES (1, 2, 3, 6);
INSERT @Table (col1, col2, col3, col4) VALUES (1, 2, 3, 7);

INSERT @Table (col1, col2, col3, col4) VALUES (4, 5, 6, 0);

INSERT @Table (col1, col2, col3, col4) VALUES (7, 8, 9, 1);
INSERT @Table (col1, col2, col3, col4) VALUES (7, 8, 9, 2);
INSERT @Table (col1, col2, col3, col4) VALUES (7, 8, 9, 3);

SELECT T1.col1, T1.col2, T1.col3, iTVF2.col4_values
FROM (
SELECT col1, col2, col3
FROM @Table
GROUP BY col1, col2, col3
HAVING COUNT(*) > 1
) T1
CROSS
APPLY (
SELECT ',' + CONVERT(VARCHAR(20), T2.col4)
FROM @Table T2
WHERE T2.col1 = T1.col1
AND T2.col2 = T2.col2
AND T2.col3 = T2.col3
FOR XML PATH('')
) iTVF1 (col4_csv)
CROSS
APPLY (
SELECT STUFF(iTVF1.col4_csv, 1, 1, SPACE(0))
) iTVF2 (col4_values);





Paul White
SQLPerformance.com
SQLblog.com
@SQL_Kiwi
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search