digitap
asked on
Select specific duplicate row based on specific criteria
I am not trained in SQL. I'm trained in other programming languages, so explain this slowly for me.
I've been beating my head against the wall with this query. I spent a couple of days on it and got the results that I wanted, but noticed today that I had duplicates rows. Essentially, it's a list of clergy members. The clergy members have multiple addresses. What I'm most concerned about is the address types 'Main\Home Address' and the 'Parsonage Address'. I was able to figure out a join statement that gets me only these two address types, but I didn't plan on some of the clergy having both, so they both show up causing my duplicates.
Essentially, if both address types are listed for a clergy member, then I only want 'Main\Home Address to appear'. Otherwise, I want those clergy that have 'Main\Home Address' and those clergy members that have 'Parsonage Address' to appear in the query. The code is below. It's an awful database, so I'm hoping that what I've typed makes sense. Again, I'm not trained in SQL, but I've tried to comment within the code so things are clear. I really hope for some ideas on what to do. Thanks for the help.
SELECT DISTINCT
OtherPersonalInformation_G CFA203.int _value AS Id,
P.first_name AS FirstName,
P.last_name AS LastName,
P.nick_name AS PreferredName,
P.middle_name AS MiddleName,
ISNULL(tb_person_suffix.lo okup_value , '') AS person_suffix,
ISNULL(tb_person_title.loo kup_value, '') AS person_title,
dbo.core_funct_primary_ema il(P.perso n_id) AS Email,
ISNULL(CELLPHONE.list_cell _phone, '') AS Phone2,
ISNULL(BUSINESSPHONE.list_ business_p hone, '') AS Phone1,
ISNULL(HOMEPHONE.list_home _phone, '') AS Phone3,
tb_AddType.lookup_value as Addtype1,
ISNULL(A.street_address_1, '') AS MailAddress1,
ISNULL(A.city,'') AS MailCity,
ISNULL(A.state, '') AS MailState,
ISNULL(A.postal_code,'') AS MailZip,
ISNULL(A.country,'') AS MailCountry,
Left(CLConferenceRelations hip_Curren tConferenc eRelations hip192.loo kup_value, 2) AS ClergyStatus,
CASE WHEN CONVERT(VARCHAR(10),ISNULL (Conferenc eRelations hip_Whendi dthecurren trelations hipbegin22 0.datetime _value,0), 101) = '01/01/1900' -- to account for accidental time
 THEN ''
 ELSE CONVERT(VARCHAR(10), ConferenceRelationship_Whe ndidthecur rentrelati onshipbegi n220.datet ime_value, 101)
END AS StatusDate,
CASE
           WHEN ISNULL(ClergyLaity_Clergy9 1.attribut e_id,'') = 91
                 THEN 'Clergy'
           WHEN ISNULL(ClergyLaity_Laity10 7.attribut e_id,'') = 107
                 THEN 'Laity'
           WHEN ISNULL(ClergyLaity_DSACLM1 74.attribu te_id,'') = 174
                 THEN 'Laity'
           END AS ClergyOrLay,
CASE
           WHEN P.record_status = 0 AND Left(CLConferenceRelations hip_Curren tConferenc eRelations hip192.loo kup_value, 2) = 'RA'
                                                  OR Left(CLConferenceRelations hip_Curren tConferenc eRelations hip192.loo kup_value, 2) = 'RD'
                                                  OR Left(CLConferenceRelations hip_Curren tConferenc eRelations hip192.loo kup_value, 2) = 'RE'
                                                  OR Left(CLConferenceRelations hip_Curren tConferenc eRelations hip192.loo kup_value, 2) = 'RP'
                                                  OR Left(CLConferenceRelations hip_Curren tConferenc eRelations hip192.loo kup_value, 2) = 'DR'
                                                  OR Left(CLConferenceRelations hip_Curren tConferenc eRelations hip192.loo kup_value, 2) = 'RL'
                                                  OR Left(CLConferenceRelations hip_Curren tConferenc eRelations hip192.loo kup_value, 2) = 'OR'
                                                  OR Left(CLConferenceRelations hip_Curren tConferenc eRelations hip192.loo kup_value, 2) = 'ROA'
                                                  OR Left(CLConferenceRelations hip_Curren tConferenc eRelations hip192.loo kup_value, 2) = 'RO'
                 THEN 'Retired'
           WHEN P.record_status = 0
                 THEN 'Active'
           WHEN P.record_status = 1 AND tb_inactive_reason.lookup_ value = 'Deceased'
                 THEN 'Deceased'
           WHEN P.record_status = 1 AND Left(CLConferenceRelations hip_Curren tConferenc eRelations hip192.loo kup_value, 2) = 'AL'
                                                       OR Left(CLConferenceRelations hip_Curren tConferenc eRelations hip192.loo kup_value, 2) = 'DI'
                                                       or Left(CLConferenceRelations hip_Curren tConferenc eRelations hip192.loo kup_value, 2) = 'HL'
                                                       OR Left(CLConferenceRelations hip_Curren tConferenc eRelations hip192.loo kup_value, 2) = 'HR'
                                                       OR Left(CLConferenceRelations hip_Curren tConferenc eRelations hip192.loo kup_value, 2) = 'SU'
                                                       OR Left(CLConferenceRelations hip_Curren tConferenc eRelations hip192.loo kup_value, 2) = 'TO'
                                                       OR Left(CLConferenceRelations hip_Curren tConferenc eRelations hip192.loo kup_value, 2) = 'WI'
                                                       OR tb_inactive_reason.lookup_ value = 'Left denomination'
                                                       OR tb_inactive_reason.lookup_ value = 'moved'
                 THEN 'Inactive'
           WHEN P.record_status = 2
                 THEN 'Pending'
           WHEN P.record_status = 3
                 THEN 'Undefined'
           END AS AccountStatus,
CASE WHEN CONVERT(VARCHAR(10),ISNULL (Conferenc eRelations hip_Whendi dthecurren trelations hipbegin22 0.datetime _value,0), 101) = '01/01/1900' -- to account for accidental time
 THEN ''
 ELSE CONVERT(VARCHAR(10), ConferenceRelationship_Whe ndidthecur rentrelati onshipbegi n220.datet ime_value, 101)
END AS AccountStatusDate,
OtherPersonalInformation_G CFA203.int _value AS GCFAId,
ISNULL(ConferenceRelations hip_GBOPHB ID92.varch ar_value,0 ) as PensionParticipationNumber ,
CASE P.gender
  WHEN 0 THEN 'Male'
  WHEN 1 THEN 'Female'
  WHEN 2 THEN 'Unknown'
  WHEN -1 THEN 'Undefined'
END AS Gender,
ISNULL(CLOtherPersonalInfo rmation_Ra ceEthnicit y183.looku p_value,0) AS Ethnicity,
CASE WHEN CONVERT(VARCHAR(10),ISNULL (P.birth_d ate,0),101 ) = '01/01/1900' -- to account for accidental time
 THEN ''
 ELSE CONVERT(VARCHAR(10), P.birth_date, 101)
END AS DateOfBirth,
tb_marital_status.lookup_v alue as MaritalStatus
FROM core_person P
LEFT OUTER JOIN core_v_phone_business BUSINESSPHONE ON businessphone.person_id = P.person_id
LEFT OUTER JOIN core_v_phone_cell CELLPHONE ON cellphone.person_id = P.person_id
LEFT OUTER JOIN core_person_attribute OtherPersonalInformation_D onotsynch2 06 ON OtherPersonalInformation_D onotsynch2 06.person_ id = P.person_id
      AND OtherPersonalInformation_D onotsynch2 06.attribu te_id = 206
LEFT OUTER JOIN core_person_attribute ConferenceRelationship_Cur rentConfer enceRelati onship192 ON ConferenceRelationship_Cur rentConfer enceRelati onship192. person_id = P.person_id
      AND ConferenceRelationship_Cur rentConfer enceRelati onship192. attribute_ id = 192
LEFT OUTER JOIN core_lookup CLConferenceRelationship_C urrentConf erenceRela tionship19 2 ON ConferenceRelationship_Cur rentConfer enceRelati onship192. int_value = CLConferenceRelationship_C urrentConf erenceRela tionship19 2.lookup_i d
      AND CLConferenceRelationship_C urrentConf erenceRela tionship19 2.organiza tion_id = 1
LEFT OUTER JOIN core_person_attribute OtherPersonalInformation_G CFA203 ON OtherPersonalInformation_G CFA203.per son_id = P.person_id
      AND OtherPersonalInformation_G CFA203.att ribute_id = 203
LEFT OUTER JOIN core_person_address PA ON PA.person_id = P.person_id AND PA.address_type_luid IN (10336,10403) --Display address types 'Main/Home Address (10403) and 'Parsonage Address' (10336)
LEFT OUTER JOIN core_address A ON A.address_id = PA.address_id Â
LEFT OUTER JOIN core_area AREA ON AREA.area_id = A.area_id
LEFT OUTER JOIN core_family_member FM ON FM.person_id = P.person_id
LEFT OUTER JOIN core_v_phone_home HOMEPHONE ON homephone.person_id = P.person_id
LEFT OUTER JOIN core_lookup tb_person_title ON tb_person_title.lookup_id = P.title_luid AND tb_person_title.organizati on_id = 1
LEFT OUTER JOIN core_lookup tb_person_suffix ON tb_person_suffix.lookup_id = P.suffix_luid AND tb_person_suffix.organizat ion_id = 1
LEFT OUTER JOIN core_lookup tb_family_role ON tb_family_role.lookup_id = FM.role_luid AND tb_family_role.organizatio n_id = 1
LEFT OUTER JOIN core_lookup tb_inactive_reason ON tb_inactive_reason.lookup_ id = P.inactive_reason_luid
      AND tb_inactive_reason.organiz ation_id = 1
LEFT OUTER JOIN core_lookup tb_Addtype ON tb_Addtype.lookup_id = PA.address_type_luid AND tb_Addtype.organization_id = 1
LEFT OUTER JOIN core_person_attribute ClergyLaity_DSACLM174 ON ClergyLaity_DSACLM174.pers on_id = P.person_id AND ClergyLaity_DSACLM174.attr ibute_id = 174
LEFT OUTER JOIN core_person_attribute ClergyLaity_Clergy91 ON ClergyLaity_Clergy91.perso n_id = P.person_id AND ClergyLaity_Clergy91.attri bute_id = 91
LEFT OUTER JOIN core_person_attribute ClergyLaity_Laity107 ON ClergyLaity_Laity107.perso n_id = P.person_id AND ClergyLaity_Laity107.attri bute_id = 107
LEFT OUTER JOIN core_person_attribute ConferenceRelationship_Whe ndidthecur rentrelati onshipbegi n220 ON ConferenceRelationship_Whe ndidthecur rentrelati onshipbegi n220.perso n_id = P.person_id AND ConferenceRelationship_Whe ndidthecur rentrelati onshipbegi n220.attri bute_id = 220
LEFT OUTER JOIN core_person_attribute ConferenceRelationship_GBO PHBID92 ON ConferenceRelationship_GBO PHBID92.pe rson_id = P.person_id AND ConferenceRelationship_GBO PHBID92.at tribute_id = 92
LEFT OUTER JOIN core_person_attribute OtherPersonalInformation_R aceEthnici ty183 ON OtherPersonalInformation_R aceEthnici ty183.pers on_id = P.person_id AND OtherPersonalInformation_R aceEthnici ty183.attr ibute_id = 183
LEFT OUTER JOIN core_lookup CLOtherPersonalInformation _RaceEthni city183 ON OtherPersonalInformation_R aceEthnici ty183.int_ value = CLOtherPersonalInformation _RaceEthni city183.lo okup_id AND CLOtherPersonalInformation _RaceEthni city183.or ganization _id = 1
LEFT OUTER JOIN core_lookup tb_marital_status ON tb_marital_status.lookup_i d = P.marital_status AND tb_marital_status.organiza tion_id = 1
--LEFT OUTER JOIN core_lookup tb_Addtype ON tb_Addtype.[lookup_id] = PA.[address_type_luid] AND tb_Addtype.organization_id = 1
WHERE
(P.organization_id = 1)
 AND ( P.record_status IN (0,1) )
 AND (P.member_status IN (
                                       12576, --Clergy
                                       12577, --Clergy Couple
                                       12580, --DSA/CLM
                                       12585 --Retired Clergy
                                       ))
AND (ISNULL(OtherPersonalInfor mation_Don otsynch206 .int_value , 0) = 0)
AND (ConferenceRelationship_Cu rrentConfe renceRelat ionship192 .int_value IN (
                                                                                                         --Active
                                                                                                         12502, --AF Affiliate member
                                                                                                         12503, --AM Associate member
                                                                                                         12504, --BH Bishop
                                                                                                         12591, --DC Deaconess
                                                                                                         12508, --DM Diaconal Minister
                                                                                                         12509, --DP Deacon recognized from other denomination servi
                                                                                                         12511, --SY District Superintendent Assignment (Supply) - DSA
                                                                                                         12513, --FD Deacon in full connection
                                                                                                         12514, --FE Elder in full connection
                                                                                                         12515, --FL Full time Local Pastor
                                                                                                         12522, --LM Certified Lay Minister
                                                                                                         12525, --OA Associate Member of other Annual Conference
                                                                                                         12526, --OD Deacon member of other annual conference
                                                                                                         12527, --OE Elder mem of other conf or Methodist denom
                                                                                                         12528, --OF Full member of other denomination
                                                                                                         12529, --OP Provisional member of other annual conference
                                                                                                         12531, --PD Provisional Deacon
                                                                                                         12532, --PE Provisional Elder
                                                                                                         12533, --PL Part time Local Pastor
                                                                                                         12553, --SP Student Local Pastor
                                                                                                         12557, --TI Transfer Membership In
                                                                                                         ----Deceased
                                                                                                         12506, --DE Deceased
                                                                                                         ----Retired
                                                                                                         12530, --OR OR Retired member of other annual conference
                                                                                                         12547, --ROA Retired Other Conference Associate Member
                                                                                                         12546, --RO Retired from Other Annual Conference, serving a
                                                                                                         12510, --DR Retired Diaconal Minister
                                                                                                         12535, --RA Retired Associate Member
                                                                                                         12537, --RD Retired Deacon in Full Connection
                                                                                                         12539, --RE Retired full member
                                                                                                         12550, --RP Retired Provisional Member
                                                                                                         12543, --RL Retired Local Pastor
                                                                                                         --Inactive
                                                                                                         12583, --AL Administrative Location
                                                                                                         12507, --DI Discontinued
                                                                                                         12518, --HL Honorable Location
                                                                                                         12520, --HR Honorable Location Retired
                                                                                                         12554, --SU Surrendered Credentials
                                                                                                         12558, --TO Transfer Membership Out
                                                                                                         12561 --WI Withdrawn
                                                                                                         ))
ORDER BY AccountStatus ASC, LastName ASC, FirstName ASC
I've been beating my head against the wall with this query. I spent a couple of days on it and got the results that I wanted, but noticed today that I had duplicates rows. Essentially, it's a list of clergy members. The clergy members have multiple addresses. What I'm most concerned about is the address types 'Main\Home Address' and the 'Parsonage Address'. I was able to figure out a join statement that gets me only these two address types, but I didn't plan on some of the clergy having both, so they both show up causing my duplicates.
Essentially, if both address types are listed for a clergy member, then I only want 'Main\Home Address to appear'. Otherwise, I want those clergy that have 'Main\Home Address' and those clergy members that have 'Parsonage Address' to appear in the query. The code is below. It's an awful database, so I'm hoping that what I've typed makes sense. Again, I'm not trained in SQL, but I've tried to comment within the code so things are clear. I really hope for some ideas on what to do. Thanks for the help.
SELECT DISTINCT
OtherPersonalInformation_G
P.first_name AS FirstName,
P.last_name AS LastName,
P.nick_name AS PreferredName,
P.middle_name AS MiddleName,
ISNULL(tb_person_suffix.lo
ISNULL(tb_person_title.loo
dbo.core_funct_primary_ema
ISNULL(CELLPHONE.list_cell
ISNULL(BUSINESSPHONE.list_
ISNULL(HOMEPHONE.list_home
tb_AddType.lookup_value as Addtype1,
ISNULL(A.street_address_1,
ISNULL(A.city,'') AS MailCity,
ISNULL(A.state, '') AS MailState,
ISNULL(A.postal_code,'') AS MailZip,
ISNULL(A.country,'') AS MailCountry,
Left(CLConferenceRelations
CASE WHEN CONVERT(VARCHAR(10),ISNULL
 THEN ''
 ELSE CONVERT(VARCHAR(10), ConferenceRelationship_Whe
END AS StatusDate,
CASE
           WHEN ISNULL(ClergyLaity_Clergy9
                 THEN 'Clergy'
           WHEN ISNULL(ClergyLaity_Laity10
                 THEN 'Laity'
           WHEN ISNULL(ClergyLaity_DSACLM1
                 THEN 'Laity'
           END AS ClergyOrLay,
CASE
           WHEN P.record_status = 0 AND Left(CLConferenceRelations
                                                  OR Left(CLConferenceRelations
                                                  OR Left(CLConferenceRelations
                                                  OR Left(CLConferenceRelations
                                                  OR Left(CLConferenceRelations
                                                  OR Left(CLConferenceRelations
                                                  OR Left(CLConferenceRelations
                                                  OR Left(CLConferenceRelations
                                                  OR Left(CLConferenceRelations
                 THEN 'Retired'
           WHEN P.record_status = 0
                 THEN 'Active'
           WHEN P.record_status = 1 AND tb_inactive_reason.lookup_
                 THEN 'Deceased'
           WHEN P.record_status = 1 AND Left(CLConferenceRelations
                                                       OR Left(CLConferenceRelations
                                                       or Left(CLConferenceRelations
                                                       OR Left(CLConferenceRelations
                                                       OR Left(CLConferenceRelations
                                                       OR Left(CLConferenceRelations
                                                       OR Left(CLConferenceRelations
                                                       OR tb_inactive_reason.lookup_
                                                       OR tb_inactive_reason.lookup_
                 THEN 'Inactive'
           WHEN P.record_status = 2
                 THEN 'Pending'
           WHEN P.record_status = 3
                 THEN 'Undefined'
           END AS AccountStatus,
CASE WHEN CONVERT(VARCHAR(10),ISNULL
 THEN ''
 ELSE CONVERT(VARCHAR(10), ConferenceRelationship_Whe
END AS AccountStatusDate,
OtherPersonalInformation_G
ISNULL(ConferenceRelations
CASE P.gender
  WHEN 0 THEN 'Male'
  WHEN 1 THEN 'Female'
  WHEN 2 THEN 'Unknown'
  WHEN -1 THEN 'Undefined'
END AS Gender,
ISNULL(CLOtherPersonalInfo
CASE WHEN CONVERT(VARCHAR(10),ISNULL
 THEN ''
 ELSE CONVERT(VARCHAR(10), P.birth_date, 101)
END AS DateOfBirth,
tb_marital_status.lookup_v
FROM core_person P
LEFT OUTER JOIN core_v_phone_business BUSINESSPHONE ON businessphone.person_id = P.person_id
LEFT OUTER JOIN core_v_phone_cell CELLPHONE ON cellphone.person_id = P.person_id
LEFT OUTER JOIN core_person_attribute OtherPersonalInformation_D
      AND OtherPersonalInformation_D
LEFT OUTER JOIN core_person_attribute ConferenceRelationship_Cur
      AND ConferenceRelationship_Cur
LEFT OUTER JOIN core_lookup CLConferenceRelationship_C
      AND CLConferenceRelationship_C
LEFT OUTER JOIN core_person_attribute OtherPersonalInformation_G
      AND OtherPersonalInformation_G
LEFT OUTER JOIN core_person_address PA ON PA.person_id = P.person_id AND PA.address_type_luid IN (10336,10403) --Display address types 'Main/Home Address (10403) and 'Parsonage Address' (10336)
LEFT OUTER JOIN core_address A ON A.address_id = PA.address_id Â
LEFT OUTER JOIN core_area AREA ON AREA.area_id = A.area_id
LEFT OUTER JOIN core_family_member FM ON FM.person_id = P.person_id
LEFT OUTER JOIN core_v_phone_home HOMEPHONE ON homephone.person_id = P.person_id
LEFT OUTER JOIN core_lookup tb_person_title ON tb_person_title.lookup_id = P.title_luid AND tb_person_title.organizati
LEFT OUTER JOIN core_lookup tb_person_suffix ON tb_person_suffix.lookup_id
LEFT OUTER JOIN core_lookup tb_family_role ON tb_family_role.lookup_id = FM.role_luid AND tb_family_role.organizatio
LEFT OUTER JOIN core_lookup tb_inactive_reason ON tb_inactive_reason.lookup_
      AND tb_inactive_reason.organiz
LEFT OUTER JOIN core_lookup tb_Addtype ON tb_Addtype.lookup_id = PA.address_type_luid AND tb_Addtype.organization_id
LEFT OUTER JOIN core_person_attribute ClergyLaity_DSACLM174 ON ClergyLaity_DSACLM174.pers
LEFT OUTER JOIN core_person_attribute ClergyLaity_Clergy91 ON ClergyLaity_Clergy91.perso
LEFT OUTER JOIN core_person_attribute ClergyLaity_Laity107 ON ClergyLaity_Laity107.perso
LEFT OUTER JOIN core_person_attribute ConferenceRelationship_Whe
LEFT OUTER JOIN core_person_attribute ConferenceRelationship_GBO
LEFT OUTER JOIN core_person_attribute OtherPersonalInformation_R
LEFT OUTER JOIN core_lookup CLOtherPersonalInformation
LEFT OUTER JOIN core_lookup tb_marital_status ON tb_marital_status.lookup_i
--LEFT OUTER JOIN core_lookup tb_Addtype ON tb_Addtype.[lookup_id] = PA.[address_type_luid] AND tb_Addtype.organization_id
WHERE
(P.organization_id = 1)
 AND ( P.record_status IN (0,1) )
 AND (P.member_status IN (
                                       12576, --Clergy
                                       12577, --Clergy Couple
                                       12580, --DSA/CLM
                                       12585 --Retired Clergy
                                       ))
AND (ISNULL(OtherPersonalInfor
AND (ConferenceRelationship_Cu
                                                                                                         --Active
                                                                                                         12502, --AF Affiliate member
                                                                                                         12503, --AM Associate member
                                                                                                         12504, --BH Bishop
                                                                                                         12591, --DC Deaconess
                                                                                                         12508, --DM Diaconal Minister
                                                                                                         12509, --DP Deacon recognized from other denomination servi
                                                                                                         12511, --SY District Superintendent Assignment (Supply) - DSA
                                                                                                         12513, --FD Deacon in full connection
                                                                                                         12514, --FE Elder in full connection
                                                                                                         12515, --FL Full time Local Pastor
                                                                                                         12522, --LM Certified Lay Minister
                                                                                                         12525, --OA Associate Member of other Annual Conference
                                                                                                         12526, --OD Deacon member of other annual conference
                                                                                                         12527, --OE Elder mem of other conf or Methodist denom
                                                                                                         12528, --OF Full member of other denomination
                                                                                                         12529, --OP Provisional member of other annual conference
                                                                                                         12531, --PD Provisional Deacon
                                                                                                         12532, --PE Provisional Elder
                                                                                                         12533, --PL Part time Local Pastor
                                                                                                         12553, --SP Student Local Pastor
                                                                                                         12557, --TI Transfer Membership In
                                                                                                         ----Deceased
                                                                                                         12506, --DE Deceased
                                                                                                         ----Retired
                                                                                                         12530, --OR OR Retired member of other annual conference
                                                                                                         12547, --ROA Retired Other Conference Associate Member
                                                                                                         12546, --RO Retired from Other Annual Conference, serving a
                                                                                                         12510, --DR Retired Diaconal Minister
                                                                                                         12535, --RA Retired Associate Member
                                                                                                         12537, --RD Retired Deacon in Full Connection
                                                                                                         12539, --RE Retired full member
                                                                                                         12550, --RP Retired Provisional Member
                                                                                                         12543, --RL Retired Local Pastor
                                                                                                         --Inactive
                                                                                                         12583, --AL Administrative Location
                                                                                                         12507, --DI Discontinued
                                                                                                         12518, --HL Honorable Location
                                                                                                         12520, --HR Honorable Location Retired
                                                                                                         12554, --SU Surrendered Credentials
                                                                                                         12558, --TO Transfer Membership Out
                                                                                                         12561 --WI Withdrawn
                                                                                                         ))
ORDER BY AccountStatus ASC, LastName ASC, FirstName ASC
ASKER
This is where my lack of sql knowledge really shines. I'm not sure how much someone needs to know to help me. Can I share the table structure without breaking confidentiality?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
In the end, I wasn't allowed to post more than what I did. The vendor, who'd not been helping, finally reported back some join statements that allowed me to come up with a solution. It's pretty much what arnold had suggested, so I'm giving him the points.
and sample data,
From your assertion, the address is not part of the individual's record, so using the referential addresstype in the group by rule will distinguish
Persona addresstype1
From
Persona addresstype2
As two separate distinct records.
....