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


Eliminate duplicate in the same column


Eliminate duplicate in the same column

Author
Message
Neel 7777
Neel 7777
SSC-Enthusiastic
SSC-Enthusiastic (110 reputation)SSC-Enthusiastic (110 reputation)SSC-Enthusiastic (110 reputation)SSC-Enthusiastic (110 reputation)SSC-Enthusiastic (110 reputation)SSC-Enthusiastic (110 reputation)SSC-Enthusiastic (110 reputation)SSC-Enthusiastic (110 reputation)

Group: General Forum Members
Points: 110 Visits: 84
I have a table which contains the field COMCOD data type nchar(4), PARTYCODE data type nchar(6) Primary Key, PARTYNAME nvarchar(250)
COMCOD PARTY CODE PARTYNAME
3305 100028 Premier Cement Mills Ltd.
3305 100029 Premier Cement Mills Ltd.
3305 100030 Premier Cement Mills Ltd.
3305 100031 Premier Cement Mills Ltd.
3305 100032 Premier Cement Mills Ltd.
3305 100033 Electro Power Engineering Ltd.
3305 100034 Electro Power Engineering Ltd.
3305 100035 Electro Power Engineering Ltd.
3305 100036 Electro Power Engineering Ltd.
3305 100037 Islam Trading Consortium Ltd.
3305 100038 Islam Trading Consortium Ltd.
3305 100039 Islam Trading Consortium Ltd.
3305 100040 Mr. Ehsanur Rahman
3305 100041 Mr. Ehsanur Rahman

I have to eliminate the duplicate party name. Can anybody help me?
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (212K reputation)SSC Guru (212K reputation)SSC Guru (212K reputation)SSC Guru (212K reputation)SSC Guru (212K reputation)SSC Guru (212K reputation)SSC Guru (212K reputation)SSC Guru (212K reputation)

Group: General Forum Members
Points: 212349 Visits: 41977
zahid_7777 (3/9/2013)
I have a table which contains the field COMCOD data type nchar(4), PARTYCODE data type nchar(6) Primary Key, PARTYNAME nvarchar(250)
COMCOD PARTY CODE PARTYNAME
3305 100028 Premier Cement Mills Ltd.
3305 100029 Premier Cement Mills Ltd.
3305 100030 Premier Cement Mills Ltd.
3305 100031 Premier Cement Mills Ltd.
3305 100032 Premier Cement Mills Ltd.
3305 100033 Electro Power Engineering Ltd.
3305 100034 Electro Power Engineering Ltd.
3305 100035 Electro Power Engineering Ltd.
3305 100036 Electro Power Engineering Ltd.
3305 100037 Islam Trading Consortium Ltd.
3305 100038 Islam Trading Consortium Ltd.
3305 100039 Islam Trading Consortium Ltd.
3305 100040 Mr. Ehsanur Rahman
3305 100041 Mr. Ehsanur Rahman

I have to eliminate the duplicate party name. Can anybody help me?


There are several different outcomes to "eliminate the duplicate party name". Please post the result set that you'd expect using the data you provided above so we can figure it out.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
SQLRNNR
SQLRNNR
SSC Guru
SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)

Group: General Forum Members
Points: 65871 Visits: 18570
Here's an article on how to do that

http://jasonbrimhall.info/2011/03/21/dedupe-data-cte/



Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw

SQLRNNR
SQLRNNR
SSC Guru
SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)

Group: General Forum Members
Points: 65871 Visits: 18570
Then again, maybe a little more description on what you seek.

Are you just trying to eliminate dupes in your output, or eliminate dupes from the table?

If you are trying to delete dupes, how do you know they are dupes? PartyCode is unique in each of those cases, what is the significance of that column?

Are these the results from a table, or are they from a view that selects from multiple tables?



Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw

Neel 7777
Neel 7777
SSC-Enthusiastic
SSC-Enthusiastic (110 reputation)SSC-Enthusiastic (110 reputation)SSC-Enthusiastic (110 reputation)SSC-Enthusiastic (110 reputation)SSC-Enthusiastic (110 reputation)SSC-Enthusiastic (110 reputation)SSC-Enthusiastic (110 reputation)SSC-Enthusiastic (110 reputation)

Group: General Forum Members
Points: 110 Visits: 84
I like to see the query as below where no duplicate party name.

COMCOD PARTY CODE PARTYNAME

3305 100028 Premier Cement Mills Ltd.

3305 100033 Electro Power Engineering Ltd.

3305 100037 Islam Trading Consortium Ltd.

3305 100040 Mr. Ehsanur Rahman
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (212K reputation)SSC Guru (212K reputation)SSC Guru (212K reputation)SSC Guru (212K reputation)SSC Guru (212K reputation)SSC Guru (212K reputation)SSC Guru (212K reputation)SSC Guru (212K reputation)

Group: General Forum Members
Points: 212349 Visits: 41977
Then the following should do it for you. Please understand that I've not actually tested the code. Please see the first link in my signature line below for why and how to get really good answers to your posts in the future.

 SELECT ComCod    = MIN(COMCOD)
, PartyCode = MIN(PartyCode)
, PartyName
FROM dbo.YourTable
GROUP BY PartyName
ORDER BY PartyName
;



--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (93K reputation)SSC Guru (93K reputation)SSC Guru (93K reputation)SSC Guru (93K reputation)SSC Guru (93K reputation)SSC Guru (93K reputation)SSC Guru (93K reputation)SSC Guru (93K reputation)

Group: General Forum Members
Points: 93693 Visits: 38955
As this is posted in a SQL Server 2005 Forum, the following will work as well:



if object_id('tempdb..#TestTable') is not null
drop table #TestTable;

create table #TestTable (
COMCOD nchar(4),
PARTYCODE nchar(6) PRIMARY KEY,
PARTYNAME nvarchar(250));

insert into #TestTable
values
('3305','100028','Premier Cement Mills Ltd.'),
('3305','100029','Premier Cement Mills Ltd.'),
('3305','100030','Premier Cement Mills Ltd.'),
('3305','100031','Premier Cement Mills Ltd.'),
('3305','100032','Premier Cement Mills Ltd.'),
('3305','100033','Electro Power Engineering Ltd.'),
('3305','100034','Electro Power Engineering Ltd.'),
('3305','100035','Electro Power Engineering Ltd.'),
('3305','100036','Electro Power Engineering Ltd.'),
('3305','100037','Islam Trading Consortium Ltd.'),
('3305','100038','Islam Trading Consortium Ltd.'),
('3305','100039','Islam Trading Consortium Ltd.'),
('3305','100040','Mr. Ehsanur Rahman'),
('3305','100041','Mr. Ehsanur Rahman');

with BaseData as (
select
COMCOD,
PARTYCODE,
PARTYNAME,
RN = ROW_NUMBER() over (partition by COMCOD, PARTYNAME order by PARTYCODE asc)
from
#TestTAble
)
select
COMCOD,
PARTYCODE,
PARTYNAME
from
BaseData
where
RN = 1
order by
COMCOD,
PARTYCODE;

if object_id('tempdb..#TestTable') is not null
drop table #TestTable;
GO




Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
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