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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
no problem :)
ASKER
I've added a order by:
SELECT RTRIM(LEFT(addresspostcode
FROM Campaign
WHERE isthisalead = 1
GROUP BY RTRIM(LEFT(addresspostcode
order by Count desc
Thanks :)