Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Count unique values in a field of a table

Status
Not open for further replies.

apestaart

Technical User
Feb 5, 2004
107
NL
Hi,
I want to count the number of unique postcodes in a table.
So 1433 GS is in 3 other records aswell, but count as one.
I need this to calculate the number of books to distribute given the fact that I only distribute one book per postcode.

Hoe Can I write a SQL statement for that function?
Best regards,
Apestaart
 
Among other ways:
Code:
SELECT Sum(cnt) AS SumOfcnt
FROM (SELECT posttable.postcode, 1 AS cnt
FROM posttable
GROUP BY posttable.postcode, 1)
;
where posttable is the name of your table and postcode is the name of your field.

Greg
"Personally, I am always ready to learn, although I do not always like being taught." - Winston Churchill
 
Code:
select count(*) from(
Select count(*) p
from posttable
GROUP BY posttable.postcode)p

No need to sum
 
Hi,
Thank you so far. But what if I add a condition like Postcode > 1500AR?
How can I add that to the SQL statement?
Regards,
Apestaart
 
SQL:
select count(*) from(
Select count(*) p
from posttable
Where Postcode > "1500AR"
GROUP BY posttable.postcode)p
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top