Showing posts with label complicated. Show all posts
Showing posts with label complicated. Show all posts

Wednesday, March 28, 2012

Finding Duplicates

I have a company table and I would like to write a query that will return to
me any duplicate companies. However, it is a little more complicated then
just matching on exact company names. I would like it to give me duplicates
where x number of letters at the beginning of the company name match AND x
number of letters of the address match AND x number of letters of the city
match. I will be doing this in batches based on the first letter of the
company name. So for example I will first process all companies that start
with the letter "A".

So for all "A" companies I want to find companies where the first 5 letters
in the company name match and the first 5 characters of the address field
match and the first 5 characters of the city match. THANKS!!!Can you post simplified DDLs, some sample data & expected results? For
detail refer to : www.aspfaq.com/5006

--
- Anith
( Please reply to newsgroups only )|||"Erich" <erich93063@.hotmail.com> wrote in message news:<102gbomj7gc84f7@.corp.supernews.com>...
> I have a company table and I would like to write a query that will return to
> me any duplicate companies. However, it is a little more complicated then
> just matching on exact company names. I would like it to give me duplicates
> where x number of letters at the beginning of the company name match AND x
> number of letters of the address match AND x number of letters of the city
> match. I will be doing this in batches based on the first letter of the
> company name. So for example I will first process all companies that start
> with the letter "A".
> So for all "A" companies I want to find companies where the first 5 letters
> in the company name match and the first 5 characters of the address field
> match and the first 5 characters of the city match. THANKS!!!

Something like this may work:

select t.*
from dbo.MyTable t
join
(
select
left(CompanyName, 5) as 'CompName',
left(Address, 5) as 'Addr',
left(City, 5) as 'City',
count(*) as 'Dupes'
from
dbo.MyTable
where
left(CompanyName, 1) = 'A'
group by
left(CompanyName, 5),
left(Address, 5),
left(City, 5)
having
count(*) > 1
) dt
on dt.CompName = left(t.CompanyName, 5)
and dt.Addr = left(t.Address, 5)
and dt.City = left(t.City, 5)

If this doesn't work as you expect, then please consider posting your
table DDL, as well as some sample data.

Simon|||This may work as well, assuming a parameter is passed into the stored proc
or function for the first letter:

SELECT C1.CompanyID, C2.CompanyID,
C1.CompanyName, C2.CompanyName,
C1.Address, C2.Address, C1.City, C2.City
FROM Company C1 JOIN Company C2 ON
LEFT(C1.CompanyName, 5) = LEFT(C2.CompanyName, 5) AND
LEFT(C1.Address, 5) = LEFT(C2.Address, 5) AND
LEFT(C1.City, 5) = LEFT(C2.City, 5) AND
C1.CompanyID != C2.CompanyID
WHERE LEFT(C1.CompanyName, 1) = @.FirstLetter

You could also use a variable parameter instead of hard-coding "5" to allow
for more specific or more general matches.
ie, ... LEFT(C1.Company, @.MatchLength) = LEFT(C2.CompanyName, @.MatchLength)

"Erich" <erich93063@.hotmail.com> wrote in message
news:102gbomj7gc84f7@.corp.supernews.com...
> I have a company table and I would like to write a query that will return
to
> me any duplicate companies. However, it is a little more complicated then
> just matching on exact company names. I would like it to give me
duplicates
> where x number of letters at the beginning of the company name match AND x
> number of letters of the address match AND x number of letters of the city
> match. I will be doing this in batches based on the first letter of the
> company name. So for example I will first process all companies that start
> with the letter "A".
> So for all "A" companies I want to find companies where the first 5
letters
> in the company name match and the first 5 characters of the address field
> match and the first 5 characters of the city match. THANKS!!!

Sunday, February 19, 2012

Filtering results

Hi,
This is a really complicated issue and is hard to explain but i have the following:

select name, MAX(table2.time) from table1 INNER JOIN table2 on table1.id = table2.id GROUP BY name

which is fine and brings up the correct results but if I want to find out from those records what another field is in table 2 for each record it pulls up too many results (i want just the one result from table 2 and then find what user it is)

if I do..

select name, table2.username MAX(table2.time) from table1 INNER JOIN table2 on table1.id = table2.id GROUP BY name, table2.username

.. it pulls up too many results cos there are different usernames

if i dont group by table2.username then it give an errorIt is not clear what do you want - what about this? May be it will help you describe your problem.

select name, max(table2.username),MAX(table2.time) from table1 INNER JOIN table2 on table1.id = table2.id GROUP BY name|||ok say i have these tables

table 1
ID Name
1 ws1
2 ws2
3 ws3
4 ws4

table 2
ID User Time
2 Bill 22/2/04 15:30
2 Bob 22/2/04 16:40
2 Bill 22/2/04 19:50
2 Jack 22/2/04 13:21
2 Jack 22/2/04 11:22
2 Jack 22/2/04 13:10
3 Jack 22/2/04 19:21
...
Inner join table 2 on table1.id = table2.id

i want to get all the records from table 1 and for each of those records i want to get the maximum Time from table 2. That i can do but I cannot extract the user name for each record because if i include the user colum in the select statement it will pul up a value for each user who was on the one machine.

Sorry i cannot explain it well|||Originally posted by steve1981
ok say i have these tables

table 1
ID Name
1 ws1
2 ws2
3 ws3
4 ws4

table 2
ID User Time
2 Bill 22/2/04 15:30
2 Bob 22/2/04 16:40
2 Bill 22/2/04 19:50
2 Jack 22/2/04 13:21
2 Jack 22/2/04 11:22
2 Jack 22/2/04 13:10
3 Jack 22/2/04 19:21
...
Inner join table 2 on table1.id = table2.id

i want to get all the records from table 1 and for each of those records i want to get the maximum Time from table 2. That i can do but I cannot extract the user name for each record because if i include the user colum in the select statement it will pul up a value for each user who was on the one machine.

Sorry i cannot explain it well

Let say for row with id=2 (table 1) you have four users - which one do you want to see in results?|||i want to get all id = 2 and the max login time of those so it would be

2 Bill 22/2/04 19:50

and i want to pull out the username which in this case would be bill|||create table #table1(id int,namen varchar(10))
create table #table2(id int,usern varchar(10),timen datetime)
go
insert #table1 values(1,'ws1')
insert #table1 values(2,'ws2')
insert #table1 values(3,'ws3')
insert #table1 values(4,'ws4')

insert #table2 values(2,'bill','2004-04-20 15:30:00.000')
insert #table2 values(2,'bob','2004-04-20 16:40:00.000')
insert #table2 values(2,'bill','2004-04-20 19:50:00.000')
insert #table2 values(2,'Jack','2004-04-20 13:21:00.000')
insert #table2 values(2,'Jack','2004-04-20 11:22:00.000')
insert #table2 values(2,'Jack','2004-04-20 13:10:00.000')
insert #table2 values(3,'Jack','2004-04-20 19:21:00.000')

select q.id,q.mt,(select top 1 usern from #table2 where id=q.id and timen=q.mt)
from
(select t1.id,max(timen) mt
from #table1 t1
join #table2 t2 on t2.id=t1.id
group by t1.id) as q