Find addresses shared by more than one customer_id where customer_name is is distinct
1
vote
1
answer
93
views
I need to return the list of addresses that have more than one customer_ID associated with them, exclude the customer_IDs that have the same customer_name and return the number of time the address repeats
Here is what the data looks like:
CUSTOMER_ID | CUSTOMER_NAME | ADDRESS | MEMBER_SINCE
----------------------------------------------------------------------
A1000 | John Doe | 123 West Lane | 2/23/2005
A1001 | John Doe | 123 West Lane | 9/30/2005
A1002 | Tom White | 456 East Street | 5/7/2006
A1003 | Frank Smith | 123 West Lane | 12/12/2006
A1004 | Lisa Simpson | 456 East Street | 7/19/2007
A1005 | Clark Kent | 700 North Ave. | 2/23/2008
A1006 | Darth Vader | 123 West Lane | 9/29/2008
A1007 | John Doe | 456 East Street | 5/23/2007
A1008 | Bart Star | 456 East Street | 4/19/2006
I want the result set to look like:
CUSTOMER_ID | CUSTOMER_NAME | ADDRESS | MEMBER_SINCE| COUNT
--------------------------------------------------------------------------------
A1000 | John Doe | 123 West Lane | 2/23/2005 | 3
A1002 | Tom White | 456 East Street | 5/7/2006 | 4
A1003 | Frank Smith | 123 West Lane | 12/12/2006 | 3
A1004 | Lisa Simpson | 456 East Street | 7/19/2007 | 4
A1006 | Darth Vader | 123 West Lane | 9/29/2008 | 3
A1007 | John Doe | 456 East Street | 5/23/2007 | 4
A1008 | Bart Star | 456 East Street | 4/19/2006 | 4
The result shows the CUSTOMER_IDs, CUSTOMER_NAMES, ADDRESSES, MEMBER_SINCE and the COUNT of distinct CUSTOMER_IDs that share the address, while ignoring the rows that have same ADDRESS and CUSTOMER_NAME as another row even though the CUSTOMER_ID is different. Any ADDRESSES that do not have more than one CUSTOMER_ID are also excluded.
Notice that CUSTOMER_ID A1001 is absent in the result since the CUSTOMER_NAME and ADDRESS are the same and must be a duplicate, and A1005 is absent since there are no other customers with that address.
This is the start:
SELECT
CUSTOMER_ID as CUSTOMER_ID,
"CUSTOMER_NAME" as CUSTOMER_NAME,
"ADDRESS",
"MEMBER_SINCE",
count(CUSTOMER_ID) as COUNTER
FROM CUSTOMER_TABLE
group by ADDRESS_ID, CUSTOMER_ID, CUSTOMER_NAME
having
count(CUSTOMER_ID)>1
order by CUSTOMER_ID;
But I can't manage to take out the duplicate CUSTOMER_NAMES.
This is my first post here and it is very late for me, so I apologize in advance if I didn't post the question correctly.
AR
Asked by user100188
(13 rep)
Jul 15, 2016, 08:43 AM
Last activity: Jul 15, 2016, 09:16 AM
Last activity: Jul 15, 2016, 09:16 AM