Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
tammy2512
Starting Member
8 Posts |
Posted - 2010-08-20 : 20:10:31
|
| Hello,Any help or advice is really appreciatedI have the following tableTable AvendornamewalmartwalmartincwalmartinccubfoodscubfoodscubfoodasperapserI need the view the count of the duplicated vendors. if I perform the followingselect vendorname,count(*) as duplicates from vendorsgroup by vendorname having count(*) >1the following o/p is displayedVendorname duplicateswalmartinc 2cubfoods 2 As we can see that (walmart, walmart inc),(asper,apser) are the same vendors; but because of the extra character or number its been considered unique; and because of which I am unable to specify the correct number of unique and duplicate vendors in the vendor table of nearly 15000 records.please advice me how can i avoid this problem; is there a way to compare two columns based on the first 7 characters.Thank you |
|
|
Devart
Posting Yak Master
102 Posts |
Posted - 2010-08-21 : 03:31:46
|
| Hello,You can try this:declare @vendor table (vendorname varchar(7),vendorchr int,vendorlen int,_count int,lvendor varchar(max))insert into @vendor (vendorname,vendorchr,vendorlen,_count)select vendorname,vendorchr,vendorlen,count(*)from( select left(Vendorname,7) as vendorname, ascii(left(Vendorname,1))+ascii(left(Vendorname,2))+ascii(left(Vendorname,3))+ascii(left(Vendorname,4))+ascii(left(Vendorname,5))+ascii(left(Vendorname,6))+ascii(left(Vendorname,7)) as vendorchr, datalength(left(Vendorname,7)) as vendorlen from <YOUR_TABLE_NAME> v where <YOUR_TABLE_NAME> ( select count(*) from where ascii(left(v.Vendorname,1))+ascii(left(v.Vendorname,2))+ascii(left(v.Vendorname,3))+ascii(left(v.Vendorname,4))+ascii(left(Vendorname,5))+ascii(left(Vendorname,6))+ascii(left(Vendorname,7))=ascii(left(Vendorname,1))+ascii(left(Vendorname,2))+ascii(left(v.Vendorname,3))+ascii(left(v.Vendorname,4))+ascii(left(v.Vendorname,5))+ascii(left(v.Vendorname,6))+ascii(left(v.Vendorname,7)) and datalength(left(Vendorname,7))=datalength(left(v.Vendorname,7)))>1) ssgroup by vendorname,vendorchr,vendorlendeclare curs cursor read_only forward_onlyfor select vendorname,vendorchr,vendorlen from @vendor order by vendorchr,vendorlen,vendornamedeclare @vm varchar(7)declare @vc intdeclare @vl intdeclare @vc_c intdeclare @vl_c intdeclare @sql varchar(max)open cursfetch next from curs into @vm,@vc,@vlset @vc_c=@vcset @vl_c=@vlset @sql='';while @@fetch_status=0 begin if @vc_c<>@vc and @vl_c<>@vl and datalength(@sql)>0 begin update @vendor set lvendor=replace('~'+@sql,'~; ','') where vendorchr=@vc_c and vendorlen=@vl_c set @sql=''; end else begin set @sql=@sql+'; '+@vm; end set @vc_c=@vc set @vl_c=@vl fetch next from curs into @vm,@vc,@vl endclose cursdeallocate cursselect vendorname, sum(_count) as vendor_countfrom (select isnull(lvendor,vendorname) as vendorname,_count from @vendor) sgroup by vendornameBest regards,Devart,SQL Server Tools:dbForge Schema ComparedbForge Data ComparedbForge Query Builder |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-08-21 : 04:03:48
|
| whats the rule for grouping then? if you're same pattern coming in more than 1 rows, you need to group them as one?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|