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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Removing Duplicate Addresses

Author  Topic 

spowell
Starting Member

23 Posts

Posted - 2008-10-01 : 19:53:43
Hi, I am really new to sql still and need a bit of help please. I am sure this is a simple problem but just not simple enough for me!!

I want to send a letter to all the addresses in my database but some addresses have 2 different customers living there. I only want to send out 1 letter to each address. The query I have come up with removes the duplicates if the clients name is the same but I can't get it to work properly if there are 2 different names at the same address.

select title, firstname, surname, address1, address2, address3, county, postcode
from addresses
group by title, firstname, surname, address1, address2, address3, county, postcode
order by postcode desc, address1


Any help would be greatly appreciated

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-10-01 : 19:56:30
What version of SQL Server are you using?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

spowell
Starting Member

23 Posts

Posted - 2008-10-01 : 19:57:40
I am using SQL Server 2005
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-02 : 03:27:31
quote:
Originally posted by spowell

Hi, I am really new to sql still and need a bit of help please. I am sure this is a simple problem but just not simple enough for me!!

I want to send a letter to all the addresses in my database but some addresses have 2 different customers living there. I only want to send out 1 letter to each address. The query I have come up with removes the duplicates if the clients name is the same but I can't get it to work properly if there are 2 different names at the same address.

select title, firstname, surname, address1, address2, address3, county, postcode
from addresses
group by title, firstname, surname, address1, address2, address3, county, postcode
order by postcode desc, address1


Any help would be greatly appreciated



modify like below to get a single customer info from each address. i dont think you are concerned with which customer you will mailing in case of more than one existing for an address.


select a.title, a.firstname, a.surname, a.address1, a.address2, a.address3, a.county, a.postcode
from addresses a
inner join(
select max(pk) as maxrec,address1, address2, address3, county, postcode
from addresses
group by address1, address2, address3, county, postcode)a1
on a1.address1=a.address1
and a1.address2=a.address2
and a1.address3=a.address3
and a1.county=a.county
and a1.postcode=a.postcode
and a1.maxrec=a.pk
order by a.postcode desc, a.address1


where pk is the primary key of address table.
Go to Top of Page

spowell
Starting Member

23 Posts

Posted - 2008-10-02 : 04:42:01
Thanks a lot for the reply but when I run the query it does execute successfully but I get 0 rows as the result.

Is there anything else I can try or have I done something wrong? Below is the query I have run

select a.title, a.firstname, a.surname, a.address1, a.address2, a.address3, a.county, a.postcode
from addresses a
inner join(
select max(postcode) as maxrec,address1, address2, address3, county, postcode
from addresses
group by address1, address2, address3, county, postcode)a1
on a1.address1=a.address1
and a1.address2=a.address2
and a1.address3=a.address3
and a1.county=a.county
and a1.postcode=a.postcode
and a1.maxrec=a.postcode
order by a.postcode desc, a.address1
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-02 : 04:45:30
quote:
Originally posted by spowell

Thanks a lot for the reply but when I run the query it does execute successfully but I get 0 rows as the result.

Is there anything else I can try or have I done something wrong? Below is the query I have run

select a.title, a.firstname, a.surname, a.address1, a.address2, a.address3, a.county, a.postcode
from addresses a
inner join(
select max(postcode) as maxrec,address1, address2, address3, county, postcodefrom addresses
group by address1, address2, address3, county, postcode)a1
on a1.address1=a.address1
and a1.address2=a.address2
and a1.address3=a.address3
and a1.county=a.county
and a1.postcode=a.postcode
and a1.maxrec=a.postcode
order by a.postcode desc, a.address1


is postcode your primary key?
then try like above.
Go to Top of Page

spowell
Starting Member

23 Posts

Posted - 2008-10-02 : 04:56:37
sorry I made a mistake with the posting. id2 is my primary key and it should of read

select a.title, a.firstname, a.surname, a.address1, a.address2, a.address3, a.county, a.postcode
from addresses a
inner join(
select max(id2) as maxrec,address1, address2, address3, county, postcode
from addresses
group by address1, address2, address3, county, postcode)a1
on a1.address1=a.address1
and a1.address2=a.address2
and a1.address3=a.address3
and a1.county=a.county
and a1.postcode=a.postcode
and a1.maxrec=a.id2
order by a.postcode desc, a.address1
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-02 : 04:59:09
quote:
Originally posted by spowell

sorry I made a mistake with the posting. id2 is my primary key and it should of read

select a.title, a.firstname, a.surname, a.address1, a.address2, a.address3, a.county, a.postcode
from addresses a
inner join(
select max(id2) as maxrec,address1, address2, address3, county, postcode
from addresses
group by address1, address2, address3, county, postcode)a1
on a1.address1=a.address1
and a1.address2=a.address2
and a1.address3=a.address3
and a1.county=a.county
and a1.postcode=a.postcode
and a1.maxrec=a.id2
order by a.postcode desc, a.address1


and didnt this work for you?
Go to Top of Page

spowell
Starting Member

23 Posts

Posted - 2008-10-02 : 05:04:32
No it executed successfully but returned no results
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-10-02 : 05:04:53
Visakh, your code will also get all customers that live alone on the address, since you have no HAVING part in the derived table.
You have not checked for multiple customers on same address.

Spowell, see this
select		title,
firstname,
surname,
address1,
address2,
address3,
county,
postcode
from (
select title,
firstname,
surname,
address1,
address2,
address3,
county,
postcode,
row_number() OVER (PArtition by address1, address2, address3, county, postcode order by title) AS recid,
count(*) OVER (PArtition by address1, address2, address3, county, postcode) AS items
from addresses
) AS d
where recid = 1
and items > 1
order by postcode desc,
address1



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-02 : 05:12:52
quote:
Originally posted by Peso

Visakh, your code will also get all customers that live alone on the address, since you have no HAVING part in the derived table.
You have not checked for multiple customers on same address.

Spowell, see this
select		title,
firstname,
surname,
address1,
address2,
address3,
county,
postcode
from (
select title,
firstname,
surname,
address1,
address2,
address3,
county,
postcode,
row_number() OVER (PArtition by address1, address2, address3, county, postcode order by title) AS recid,
count(*) OVER (PArtition by address1, address2, address3, county, postcode) AS items
from addresses
) AS d
where recid = 1
and items > 1
order by postcode desc,
address1



E 12°55'05.63"
N 56°04'39.26"



But wasnt that the requirement? to make sure only one letter goes to each address. so if its only one customer, send to him and if more tham one, send only one of them.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-10-02 : 05:17:28
You seem to be right.
I interpreted the assignment that he wanted to send out a letter to one customer for each address where the address have more than 1 customer.

If you are right, just delete the Item lines (2) in ny suggestion.



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-02 : 05:20:12
quote:
Originally posted by Peso

You seem to be right.
I interpreted the assignment that he wanted to send out a letter to one customer for each address where the address have more than 1 customer.

If you are right, just delete the Item lines (2) in ny suggestion.



E 12°55'05.63"
N 56°04'39.26"



Yup. and problem OP faces seems to be something else. As OP is telling its not retrieving any results.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-10-02 : 05:35:24
NULLs, maybe?
JOINing on NULLs is not a good idea.

ROW_NUMBER() does not have that problem.



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-02 : 05:39:34
quote:
Originally posted by Peso

NULLs, maybe?
JOINing on NULLs is not a good idea.

ROW_NUMBER() does not have that problem.



E 12°55'05.63"
N 56°04'39.26"



but i was taking max valu of primary key column for each group and joining with that. but that wont NULLs will it?
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-10-02 : 05:43:06
See post made 10/02/2008 : 04:56:37
Should be
select		a.title,
a.firstname,
a.surname,
a.address1,
a.address2,
a.address3,
a.county,
a.postcode
from addresses as a
inner join (
select max(id2) as maxrec
from addresses
group by address1,
address2,
address3,
county,
postcode
) as a1 on a1.maxrec = a.id2
order by a.postcode desc,
a.address1
instead, which is the same as ROW_NUMBER does.
select		title,
firstname,
surname,
address1,
address2,
address3,
county,
postcode
from (
select title,
firstname,
surname,
address1,
address2,
address3,
county,
postcode,
row_number() OVER (PArtition by address1, address2, address3, county, postcode order by title) AS recid
from addresses
) AS d
where recid = 1
order by postcode desc,
address1



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-02 : 05:48:32
Ah...i see what you mean now. Thanks for making it clear.
Go to Top of Page

spowell
Starting Member

23 Posts

Posted - 2008-10-02 : 08:40:36
Thanks to both of you for all of your help. Have managed to get it to work now with the 1st option of your last post Peso. I did try the 2nd option as well but got the following error

Msg 195, Level 15, State 10, Line 18
'row_number' is not a recognized function name.

Not sure if that was just me making a mistake though

Thanks again
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-10-02 : 08:44:36
For ROW_NUMBER() to work you must

1) Use SQL Server 2005 or later
2) Have compatibility mode set to 90 or higher


E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-02 : 12:15:21
quote:
Originally posted by spowell

Thanks to both of you for all of your help. Have managed to get it to work now with the 1st option of your last post Peso. I did try the 2nd option as well but got the following error

Msg 195, Level 15, State 10, Line 18
'row_number' is not a recognized function name.

Not sure if that was just me making a mistake though

Thanks again


run this in your query window and see what it returns
SELECT SERVERPROPERTY('productversion')
Go to Top of Page

spowell
Starting Member

23 Posts

Posted - 2008-10-02 : 16:09:29
It returned

8.00.2039
Go to Top of Page
    Next Page

- Advertisement -