Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Remove Duplicates from a Table using specific columns


Remove Duplicates from a Table using specific columns

Author
Message
Babbi
Babbi
Forum Newbie
Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)

Group: General Forum Members
Points: 1 Visits: 4
Hi Gurus,

I am new to Teradata and working on a requirement.

Below is the table

Col1 Col2 Col3 Col4

A Y 1 Z
B Y 2 Q
C Y 3 R
A Y 4 Z
A Y 5 Z


Output as:

Col1 Col2 Col3 Col4

A Y 1 Z
B Y 2 Q
C Y 3 R


Please guide me resolving this issue.

Thanks,
LG
raym85
raym85
Old Hand
Old Hand (376 reputation)Old Hand (376 reputation)Old Hand (376 reputation)Old Hand (376 reputation)Old Hand (376 reputation)Old Hand (376 reputation)Old Hand (376 reputation)Old Hand (376 reputation)

Group: General Forum Members
Points: 376 Visits: 198
if you're working on a huge amount of data I think the most optimized way would be to use the row)number function. This allows you to partition the values you need to determine to be unique.

In the query I have written below you can see that by partitioning you are assigning unique values to those columns in the partition. If you come across two values that are the same the partition will assign a new number for that value. The outer query where clause is important in this case because by choosing one you are choosing to only select those that are non duplicated.

njoy!



CREATE TABLE #temp
(
Col1 varchar(100),
Col2 varchar(100) ,
Col3 varchar(100) ,
Col4 varchar(100)
)


INSERT INTO #temp
VALUES ('A', 'Y', '1', 'Z'),
('B', 'Y', '2', 'Q'),
('C', 'Y', '3', 'R'),
('A', 'Y', '4', 'Z'),
('A', 'Y', '5', 'Z')


SELECT Col1,
Col2,
Col3,
Col4 FROM (
select ROW_NUMBER()OVER (PARTITION BY Col1 order by COL1) ROWID,
Col1,
Col2,
Col3,
Col4
FROM #temp)LL
where ROWID = 1
Sergiy
Sergiy
SSCertifiable
SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)

Group: General Forum Members
Points: 5846 Visits: 11406
A Y 1 Z


What is the rule for leaving this particular record, not any of the others with "A Y"?
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