Link to home
Start Free TrialLog in
Avatar of Joel Buhr
Joel BuhrFlag for United States of America

asked on

Count Distinct Records by Address MySQL

I have a database where I know that I may have more than one record present for a given Address, Individual, or Surname+Address (HOUSEHOLD)

The data in the table is standardized. Fields include :
EMAIL      
FIRST NAME
LAST NAME
ADDRESS
CITY      
STATE      
ZIPCODE
URL
CODE

I need to be able to run a query by either URL, CODE, State, AND ZIP
For Example
WHERE URL LIKE '%motorcycle%'
and STATE in ('mi', 'oh')
GROUP BY STATE

Open in new window


or
WHERE CODE IN ('abcd', 'xyz')
GROUP BY STATE

Open in new window


But I need to return a count or even pull the data based on ONE record per Household (household being defined as : Surname, Address, City, State, ZIP), or one per Address (Address being defined as : Address, City, State, ZIP)

How would I do this in combination with the other selects
Avatar of James0628
James0628

The obvious solution would seem to be to group by the required fields.  If you want one record per "household", group by Surname, Address, City, State and ZIP.  If you want a count of the records for each "household", include Count in the query (I'm assuming that MySQL has a Count function, or something similar).

 I'm not sure how the two different Where's (checking URL and STATE, or checking CODE), or the two different "groups" (Household or Address) fit in.  Is the user going to be selecting which fields they're testing and which "group" they're looking for?  Are you trying to handle all of those possibilities in one query?

 James
ASKER CERTIFIED SOLUTION
Avatar of Zberteoc
Zberteoc
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
We have had to deal with a very similar issue in our constituent management app.  The problem boils down to two things:
There are an infinite number of ways to write a single address.  Between typos, non-standard abbreviations, and just plain wrongness, you can never be sure that any two addresses point to the same place.  We work around this (to a degree) with an in-house application which leverages USPS official address lists and standardization techniques.  Every address we receive is initially sent through this app for formatting and term standardization, and verification of physical existence (according to the USPS, anyways).
Your firm definition of "household" is a good thing, but a bit complicated to be using "on the fly", so to speak.  It might be better to abstract this information to a "household identifier" table.  For example, you could move any address-specific fields in YourTable to AddressIDTable, replacing them with the ID field from the new lookup.  This would allow for a much simpler comparison to decide if two people are living at the same address - if the IDs match, then yes.

Hope this helps!
Avatar of Joel Buhr

ASKER

Zberteoc

Thank you for your response I will try that. The examples is what I was looking for.

Steve Bink
Very helpful suggestions. I will look into this.

James0628
Although I appreciate your response to this question. I do realize that if I want one per household that it is a combination of a few fields. What I did not know how to do is create the correct query.  To answer your question. We are searching by URL or Code to select specific people in the table by what code has been assigned to them or by the URL they came in on.  Its one thing to simply write the query for that, but another to limit the results to one per Address or one per Household in combination with the other.
Since you were already using Group By, adding more fields to that seemed straightforward enough (which made me think that there might be more to the question).

 As for the counts, I was originally thinking of counting the records within each Household/Address (eg. to identify the Households that have multiple records).  If you want to count the number of unique Households/Addresses, you can combine the fields that make up a Household/Address and do a count on that.  For example, for a Household count, assuming that ZIP is a numeric field, it might be something like

COUNT (DISTINCT Surname, Address, City, State, CAST (ZIP AS CHAR)) AS Count_Households

 James