| Author |
Topic |
|
dhani
Posting Yak Master
132 Posts |
Posted - 2008-10-24 : 17:53:43
|
| Hello All,in my Sql Server(2005) Table, i have data like belowAcct#, Acct Name, BK #, Addr1, Stat#123,Jr Borello,45,Pau St,345321124,Ketrina,45,Light DR,341121125,Steve,51,Corr Dr St,445323123,Anand,45,Sell St,345321121,Myaham,41,Park Ave,767333from the above data,how can i remove the duplicate rows(here in this example duplicate rows means, need not to be same in all column values), but if Acct#,BK#,Stat# are same(ex: 1st & 4th rows) then how can i remove it?can any one please assist meThanks in advanceasin |
|
|
hanbingl
Aged Yak Warrior
652 Posts |
Posted - 2008-10-24 : 18:25:43
|
Sample data and querydeclare @sample table (Acct int, AcctName varchar(50), BKno int, Addr1 varchar(50), Stat varchar(10))insert into @sampleselect 123,'Jr Borello',45,'Pau St',345321 union allselect 124,'Ketrina',45,'Light DR',341121 union allselect 125,'Steve',51,'Corr Dr St',445323 union allselect 123,'Anand',45,'Sell St',345321 union allselect 121,'Myaham',41,'Park Ave',767333delete from @samplefrom @sample a, (selectacct, acctname,bkno,addr1,statfrom (selectacct, acctname,bkno,addr1,stat,row_number() over (partition by acct, bkno, stat order by stat) as rowid,count(*) OVER (PArtition by acct, bkno, stat) as cntfrom @sample)t where rowid=1 and cnt>1)bwhere a.acct = b.acct and a.acctname = b.acctnameand a.bkno = b.bkno and a.addr1 = b.addr1 and a.stat = b.statselect * from @sample |
 |
|
|
dhani
Posting Yak Master
132 Posts |
Posted - 2008-10-24 : 19:30:55
|
| Hello hanbingl,Thank you for your fastest reply,seems to be here you are manually inserting the rows into sample tableanyway i will work on it and let you knowonce again thank you very much for your replyBest Regardsasin |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-25 : 01:57:58
|
hanbingl is not manually inserting records. The initial population part is just to create a sample data for your scenario. As you've existing table with data, you need to do only the delete query part.Also please note that you should be using sql 2005 or later if you want to use row_number() function. just in case you're using sql 2000 or below, use the below querydelete t1 from yourtable t1left join (select [Acct#], MAX([Acct Name]) AS latest, [BK #], [Stat#] from yourtable group by [Acct#],[BK #], [Stat#])t2on t2.[Acct#] = t1.[Acct#]AND t2.latest =t1.[Acct Name]AND t2.[BK #] =t1.[BK #]AND t2.[Stat#]=t1.[Stat#]WHERE t1.[Acct#] IS NULL |
 |
|
|
dhani
Posting Yak Master
132 Posts |
Posted - 2008-10-25 : 15:37:41
|
Hello Visakh, Thank you for your reply,as i issued the above command it says remove 0 rows,however if i use only below command the result is (eliminating of duplicates) but if use whole your command it says 0 rows to delete quote: select [Acct#], MAX([Acct Name]) AS latest, [BK #], [Stat#] from yourtable group by [Acct#],[BK #], [Stat#]
can you please look at it onceThanks & Regardsasin |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-26 : 01:17:35
|
what does the below return you?select [Acct#], [BK #], [Stat#] from yourtable group by [Acct#],[BK #], [Stat#]HAVING COUNT(*) >1 |
 |
|
|
dhani
Posting Yak Master
132 Posts |
Posted - 2008-10-26 : 23:45:48
|
quote: Originally posted by visakh16 what does the below return you?select [Acct#], [BK #], [Stat#] from yourtable group by [Acct#],[BK #], [Stat#]HAVING COUNT(*) >1
it returns one rowActual Table Data123 Andi 45 Lpuram 4353123 Boochek 45 Kjamna 3222345 Rjqsa 23 Vpuram 1234456 mhsan 64 sydney 7384345 asin 23 usa 1234with below sql statement it returns one query quote: select [Acct], [BKno], [Stat] from v2ex group by [Acct],[BKno], [Stat]HAVING COUNT(*) >1
results below row345 23 1234 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-27 : 00:06:10
|
quote: Originally posted by dhani
quote: Originally posted by visakh16 what does the below return you?select [Acct#], [BK #], [Stat#] from yourtable group by [Acct#],[BK #], [Stat#]HAVING COUNT(*) >1
it returns one rowActual Table Data123 Andi 45 Lpuram 4353123 Boochek 45 Kjamna 3222345 Rjqsa 23 Vpuram 1234456 mhsan 64 sydney 7384345 asin 23 usa 1234with below sql statement it returns one query quote: select [Acct], [BKno], [Stat] from v2ex group by [Acct],[BKno], [Stat]HAVING COUNT(*) >1
results below row345 23 1234 then this should delete one row of that groupdelete t1 from yourtable t1left join (select [Acct#], MAX([Acct Name]) AS latest, [BK #], [Stat#] from yourtable group by [Acct#],[BK #], [Stat#])t2on t2.[Acct#] = t1.[Acct#]AND t2.latest =t1.[Acct Name]AND t2.[BK #] =t1.[BK #]AND t2.[Stat#]=t1.[Stat#]WHERE t1.[Acct#] IS NULL |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-27 : 00:15:50
|
you can even try this:-delete tfrom(select row_number() over (partition by [Acct#],[BK #],[Stat#] order by [Acct Name] desc) as seq,*from yourtable)twhere t.seq>1 |
 |
|
|
dhani
Posting Yak Master
132 Posts |
Posted - 2008-10-28 : 10:59:51
|
quote: Originally posted by visakh16 you can even try this:-delete tfrom(select row_number() over (partition by [Acct#],[BK #],[Stat#] order by [Acct Name] desc) as seq,*from yourtable)twhere t.seq>1
Hello visakh,this is worked wonder,i am very glad,Thank you very much, this what i am looking forBest Regardsasin |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-28 : 11:10:49
|
quote: Originally posted by dhani
quote: Originally posted by visakh16 you can even try this:-delete tfrom(select row_number() over (partition by [Acct#],[BK #],[Stat#] order by [Acct Name] desc) as seq,*from yourtable)twhere t.seq>1
Hello visakh,this is worked wonder,i am very glad,Thank you very much, this what i am looking forBest Regardsasin
welcome |
 |
|
|
|
|
|