Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SQL count distinct

Posted on 2011-09-27
7
Medium Priority
?
539 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
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
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

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!

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.
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

609 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