Solved

SQL count distinct

Posted on 2011-09-27
7
499 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

706 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now