Link to home
Start Free TrialLog in
Avatar of XanderP
XanderP

asked on

Search for top post codes

ok, I have a nvarchar(10) field to store a UK postcode, such as DK3 7VV, or LL13 5GJ

What Select quert do I need to group all postcodes by their areas, i.e. DK3  or LL13?

Say I have a data set of:

DK3 5HH
DL2 1HK
DK3 7JS
LL13 5HK
LL13 6SD
DK3 6AS


It would return

Area        Count
DK3         3
LL13        2
DL2         1



I have this code at the moment:

select COUNT(uniquekey) AS COUNT, left(addresspostcode,4) As AddressPostCode
from campaign
where isthisalead=1
GROUP BY addresspostcode

but it's looking at the postcode before it is truncated, so it evaluating the full string before it groups it...I want to truncate it then group it....

does this make sense?

TIA
ASKER CERTIFIED SOLUTION
Avatar of adathelad
adathelad
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of XanderP
XanderP

ASKER

Looks spot on adathelad :)


I've added a order by:


SELECT RTRIM(LEFT(addresspostcode,4)) AS AddressPostCode, COUNT(1) as Count
FROM Campaign
WHERE isthisalead = 1
GROUP BY RTRIM(LEFT(addresspostcode,4))
order by Count desc



Thanks :)
no problem :)