?
Solved

SQL count distinct

Posted on 2011-09-27
7
Medium Priority
?
527 Views
Last Modified: 2012-06-27
Hi,

I have a table that has fields in it like so

HA7 5PQ
HA8 6ZA
WA19 7ZE
WA20 4AZ
HA5 5AE
HA8 6RT
WA19 7ZA

and so on

I want to do a query to count all distinct postcodes excluding the last 3 digits so for the example above would look like this

HA7
HA8
WA19
WA20
HA5

Which means there are 5 distinct data

The table that it is stored in is postcode and the column is called codes

please help

Thanks,

R8VI  
0
Comment
Question by:R8VI
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 31

Accepted Solution

by:
James Murrell earned 2000 total points
ID: 36708219
something like

select distinct left(postcode,4) from codes
0
 
LVL 9

Expert Comment

by:mimran18
ID: 36708233
Select  Left([Field Name],3) from [Table Name] Group By Left([Field name],3)
0
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 36708265
select distinct substring(postcode, 1, (len(postcode) - 3))  from codes
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
LVL 19

Expert Comment

by:Bhavesh Shah
ID: 36708356
Hi,

I think you wanted to separate data based on " " space.

if so, check out this code.

declare @str varchar(20)
set @str = 'ADDSA7 5PDAQ'


SELECT substring(@str,1,charindex(' ',@str)-1)

Open in new window


- bhavesh

0
 
LVL 18

Expert Comment

by:deighton
ID: 36708396
select  count(distinct RTRIM(LEFT(RTRIM(codes ),LEN(RTRIM(codes )) - 3))) from postcode WHERE LEN(RTRIM(codes )) > 3;
0
 
LVL 2

Expert Comment

by:mansooralia_yahoo
ID: 36708440
Hi,

I think bhavesh is on right track, but need to add distinct, just like

declare @str varchar(20)
set @str = 'ADDSA7 5PDAQ'


SELECT distinct substring(@str,1,charindex(' ',@str)-1)

Open in new window

0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 36709120
;with cte as
 ( select left(ltrim(codes)+' ',charindex(' ',ltrim(codes)+' ') -1) as OutwardPostcode
    from postcodes)
select outwardpostcode,count(*)
from cte
group by outwardpostcode
order by 1

or

;with cte as
 ( select left(ltrim(codes)+' ',charindex(' ',ltrim(codes)+' ') -1) as OutwardPostcode
    from postcodes)
select count(distinct outwardpostcode)
from cte


ps you should really store the postcode as its component parts  at least the outward and inward even if you don't go down to the sector and walk layers...  you can always have a computed column on the table or view containing the whole string




0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have a large data set and a SSIS package. How can I load this file in multi threading?
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question