Solved

SQL count distinct

Posted on 2011-09-27
7
506 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
7 Comments
 
LVL 31

Accepted Solution

by:
James Murrell earned 500 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to shrink a transaction log file down to a reasonable size.

803 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