Solved

SQL count distinct

Posted on 2011-09-27
7
511 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 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.

Question has a verified solution.

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

Suggested Solutions

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

730 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