| 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, postcodefrom addressesgroup by title, firstname, surname, address1, address2, address3, county, postcodeorder by postcode desc, address1Any help would be greatly appreciated |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
spowell
Starting Member
23 Posts |
Posted - 2008-10-01 : 19:57:40
|
| I am using SQL Server 2005 |
 |
|
|
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, postcodefrom addressesgroup by title, firstname, surname, address1, address2, address3, county, postcodeorder by postcode desc, address1Any 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.postcodefrom addresses ainner join(select max(pk) as maxrec,address1, address2, address3, county, postcodefrom addressesgroup by address1, address2, address3, county, postcode)a1on a1.address1=a.address1and a1.address2=a.address2and a1.address3=a.address3and a1.county=a.countyand a1.postcode=a.postcodeand a1.maxrec=a.pkorder by a.postcode desc, a.address1 where pk is the primary key of address table. |
 |
|
|
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 runselect a.title, a.firstname, a.surname, a.address1, a.address2, a.address3, a.county, a.postcodefrom addresses ainner join(select max(postcode) as maxrec,address1, address2, address3, county, postcodefrom addressesgroup by address1, address2, address3, county, postcode)a1on a1.address1=a.address1and a1.address2=a.address2and a1.address3=a.address3and a1.county=a.countyand a1.postcode=a.postcodeand a1.maxrec=a.postcodeorder by a.postcode desc, a.address1 |
 |
|
|
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 runselect a.title, a.firstname, a.surname, a.address1, a.address2, a.address3, a.county, a.postcodefrom addresses ainner join(select max(postcode) as maxrec,address1, address2, address3, county, postcodefrom addressesgroup by address1, address2, address3, county, postcode)a1on a1.address1=a.address1and a1.address2=a.address2and a1.address3=a.address3and a1.county=a.countyand a1.postcode=a.postcodeand a1.maxrec=a.postcodeorder by a.postcode desc, a.address1
is postcode your primary key? then try like above. |
 |
|
|
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 readselect a.title, a.firstname, a.surname, a.address1, a.address2, a.address3, a.county, a.postcodefrom addresses ainner join(select max(id2) as maxrec,address1, address2, address3, county, postcodefrom addressesgroup by address1, address2, address3, county, postcode)a1on a1.address1=a.address1and a1.address2=a.address2and a1.address3=a.address3and a1.county=a.countyand a1.postcode=a.postcodeand a1.maxrec=a.id2order by a.postcode desc, a.address1 |
 |
|
|
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 readselect a.title, a.firstname, a.surname, a.address1, a.address2, a.address3, a.county, a.postcodefrom addresses ainner join(select max(id2) as maxrec,address1, address2, address3, county, postcodefrom addressesgroup by address1, address2, address3, county, postcode)a1on a1.address1=a.address1and a1.address2=a.address2and a1.address3=a.address3and a1.county=a.countyand a1.postcode=a.postcodeand a1.maxrec=a.id2order by a.postcode desc, a.address1
and didnt this work for you? |
 |
|
|
spowell
Starting Member
23 Posts |
Posted - 2008-10-02 : 05:04:32
|
| No it executed successfully but returned no results |
 |
|
|
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 thisselect title, firstname, surname, address1, address2, address3, county, postcodefrom ( 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 dwhere recid = 1 and items > 1order by postcode desc, address1 E 12°55'05.63"N 56°04'39.26" |
 |
|
|
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 thisselect title, firstname, surname, address1, address2, address3, county, postcodefrom ( 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 dwhere recid = 1 and items > 1order 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. |
 |
|
|
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" |
 |
|
|
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. |
 |
|
|
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" |
 |
|
|
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? |
 |
|
|
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 beselect a.title, a.firstname, a.surname, a.address1, a.address2, a.address3, a.county, a.postcodefrom addresses as ainner join ( select max(id2) as maxrec from addresses group by address1, address2, address3, county, postcode ) as a1 on a1.maxrec = a.id2order by a.postcode desc, a.address1 instead, which is the same as ROW_NUMBER does.select title, firstname, surname, address1, address2, address3, county, postcodefrom ( 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 dwhere recid = 1order by postcode desc, address1 E 12°55'05.63"N 56°04'39.26" |
 |
|
|
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. |
 |
|
|
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 errorMsg 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 thoughThanks again |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-10-02 : 08:44:36
|
For ROW_NUMBER() to work you must1) Use SQL Server 2005 or later2) Have compatibility mode set to 90 or higher E 12°55'05.63"N 56°04'39.26" |
 |
|
|
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 errorMsg 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 thoughThanks again
run this in your query window and see what it returnsSELECT SERVERPROPERTY('productversion') |
 |
|
|
spowell
Starting Member
23 Posts |
Posted - 2008-10-02 : 16:09:29
|
| It returned8.00.2039 |
 |
|
|
Next Page
|
|
|