|
Home > Archive > Microsoft SQL Server forum > March 2006 > Join returns more than one row, Post code regular expressions
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Join returns more than one row, Post code regular expressions
|
|
| cheesey_toastie 2006-03-23, 7:38 am |
| Hi,
I trying to write a select statement that will return each of my sales
men a region code based on a table of post codes using wildcards... eg.
MK1 1AA would be matched in the region code table to MK1%
SELECT dn.DEALER_CODE, dn.NAME AS DNAME, rc.REGION_ID,
rc.POST_CODE, dn.POSTAL_CODE
FROM REGIONAL_CODES rc CROSS JOIN
DEALER_NAW dn
WHERE (dn.POSTAL_CODE LIKE rc.POST_CODE)
The above statement works BUT there are some post code areas such as
our friends in Milton Keynes that are split into two regions... eg MK1
is region id 2 and MK10 is region 3.
So a dealer with post code MK10 1AA would be matched to both rows
returning duplicates
POST_CODE REGION_ID
MK1% 2
MK10% 3
I think the answer would lie in a subquery which returns the ID of the
region with the longest length of the postcode match (e.g.
len(POST_CODE) for the rc table... return only the MAX....
any ideas????
Any help muchos appreciated, and I apologies now for the naming of the
dealers name as a reserve word... not me!
Ct
| |
| Damien 2006-03-23, 7:38 am |
| cheesey_toastie wrote:
> Hi,
>
> I trying to write a select statement that will return each of my sales
> men a region code based on a table of post codes using wildcards... eg.
> MK1 1AA would be matched in the region code table to MK1%
>
> SELECT dn.DEALER_CODE, dn.NAME AS DNAME, rc.REGION_ID,
> rc.POST_CODE, dn.POSTAL_CODE
> FROM REGIONAL_CODES rc CROSS JOIN
> DEALER_NAW dn
> WHERE (dn.POSTAL_CODE LIKE rc.POST_CODE)
>
> The above statement works BUT there are some post code areas such as
> our friends in Milton Keynes that are split into two regions... eg MK1
> is region id 2 and MK10 is region 3.
>
> So a dealer with post code MK10 1AA would be matched to both rows
> returning duplicates
> POST_CODE REGION_ID
> MK1% 2
> MK10% 3
>
> I think the answer would lie in a subquery which returns the ID of the
> region with the longest length of the postcode match (e.g.
> len(POST_CODE) for the rc table... return only the MAX....
>
> any ideas????
>
> Any help muchos appreciated, and I apologies now for the naming of the
> dealers name as a reserve word... not me!
>
> Ct
Hi Ct,
I think it would be along the lines of:
SELECT dn.DEALER_CODE, dn.NAME AS DNAME, rc.REGION_ID,
rc.POST_CODE, dn.POSTAL_CODE
FROM REGIONAL_CODES rc CROSS JOIN
DEALER_NAW dn
WHERE (dn.POSTAL_CODE LIKE rc.POST_CODE) and not exists (select *
from REGIONAL_CODES rc2 where dn.POSTAL_CODE LIKE rc2.POST_CODE and
LEN(rc2.POSTAL_CODE) > LEN(rc.POSTAL_CODE))
Damien
| |
| cheesey_toastie 2006-03-23, 1:31 pm |
| Hi Damien,
I was first initially dubious that it would work where the post code
matched three different rows e.g.
If postal_code = MK111 1AA ....
MK%
MK1%
MK11%
But it does, and a quick read of
http://www.techonthenet.com/sql/exists.php explained it.
Thanks for that!
Below is the code with the minor corrections of the field names for
anyone following the post...
SELECT dn.DEALER_CODE, dn.NAME AS DNAME, rc.REGION_ID,
rc.POST_CODE, dn.POSTAL_CODE
FROM REGIONAL_CODES rc CROSS JOIN
DEALER_NAW dn
WHERE (dn.POSTAL_CODE LIKE rc.POST_CODE) AND (NOT EXISTS
(SELECT *
FROM REGIONAL_CODES rc2
WHERE dn.POSTAL_CODE LIKE
rc2.POST_CODE AND LEN(rc2.POST_CODE) > LEN(rc.POST_CODE)))
ct
|
|
|
|
|