Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fetch and find frquency count of generalized zipcode

Tags:

mysql

group-by

I have table as below

ID NAME AGE ZIPCODE
1  A    29   321345
2  B    25   321375
....

and so on about 40K records.

I want to fetch distinct zipcodes only upto 4 digits.

like 3213* include both (321345 || 321375).

So is there any similar clause as GROUP BY for this?

like image 667
user2134 Avatar asked Oct 19 '22 15:10

user2134


1 Answers

If only distinct zipcodes(4-digit) is need, use this:

SELECT DISTINCT LEFT(zipcodes, 4) ZIPCODE_4_DIGIT FROM tbl

If the frequency is also needed, use another:

SELECT LEFT(zipcodes, 4) ZIPCODE_4_DIGIT, COUNT(1) FREQUENCY 
FROM tbl
GROUP BY ZIPCODE_4_DIGIT;
like image 159
Dylan Su Avatar answered Oct 21 '22 08:10

Dylan Su