where
HistoricalDataRecordId, TerminalId, UserEmail, CustomerName, AbilityAccountId, Type, First, Middle, Name, Suffix, DoingBusinessAs, Description, Address1, Address2, City, State, Zip, Country, Phone, EIN, NPI, PhoneExtension, Email, Fax, BillingAddress1, BillingAddress2, BillingCity, BillingState, BillingZip, BillingCountry, PrimaryCommunicationContact, DefaultAuthorizedEnrollmentSigner, SecondaryCommunicationContact, AdditionalAuthorizedSigners, EntityId, claimsManagementProPartnerAlias
is distinct.
where EntityId is combined between where ever HistoricalDataRecordId, TerminalId, UserEmail, CustomerName, AbilityAccountId, Type, First, Middle, Name, Suffix, DoingBusinessAs, Description, Address1, Address2, City, State, Zip, Country, Phone, EIN, NPI, PhoneExtension, Email, Fax, BillingAddress1, BillingAddress2, BillingCity, BillingState, BillingZip, BillingCountry, PrimaryCommunicationContact, DefaultAuthorizedEnrollmentSigner, SecondaryCommunicationContact, AdditionalAuthorizedSigners, EntityId, claimsManagementProPartnerAlias matches?
so if there were two rows with that data that is the same but
row 1 EI:156000470
row 2 NULL
then there would be only one row with entitytype = EI:156000470
WITH DistinctRows AS (
SELECT
*,
ROW_NUMBER() OVER (PARTITION BY
HistoricalDataRecordId, TerminalId, UserEmail, CustomerName, AbilityAccountId, Type,
First, Middle, Name, Suffix, DoingBusinessAs, Description, Address1, Address2, City, State, Zip, Country,
Phone, EIN, NPI, PhoneExtension, Email, Fax, BillingAddress1, BillingAddress2, BillingCity, BillingState,
BillingZip, BillingCountry, PrimaryCommunicationContact, DefaultAuthorizedEnrollmentSigner,
SecondaryCommunicationContact, AdditionalAuthorizedSigners, claimsManagementProPartnerAlias
ORDER BY rownum) AS RowNum
FROM #DistinctDataWithMaxRowNumber
),
AggregatedEntityIds AS (
SELECT
MAX(rownum) AS MaxRowNum, -- Retain the highest rownum for consistency
STRING_AGG(EntityId, '~') WITHIN GROUP (ORDER BY EntityId) AS AggregatedEntityId
FROM DistinctRows
WHERE EntityId IS NOT NULL
GROUP BY
HistoricalDataRecordId, TerminalId, UserEmail, CustomerName, AbilityAccountId, Type,
First, Middle, Name, Suffix, DoingBusinessAs, Description, Address1, Address2, City, State, Zip, Country,
Phone, EIN, NPI, PhoneExtension, Email, Fax, BillingAddress1, BillingAddress2, BillingCity, BillingState,
BillingZip, BillingCountry, PrimaryCommunicationContact, DefaultAuthorizedEnrollmentSigner,
SecondaryCommunicationContact, AdditionalAuthorizedSigners, claimsManagementProPartnerAlias,
RowNum
)
SELECT
dr.rownum,
dr.HistoricalDataRecordId,
dr.TerminalId,
dr.UserEmail,
dr.CustomerName,
dr.AbilityAccountId,
dr.Type,
dr.First,
dr.Middle,
dr.Name,
dr.Suffix,
dr.DoingBusinessAs,
dr.Description,
dr.Address1,
dr.Address2,
dr.City,
dr.State,
dr.Zip,
dr.Country,
dr.Phone,
dr.EIN,
dr.NPI,
dr.PhoneExtension,
dr.Email,
dr.Fax,
dr.BillingAddress1,
dr.BillingAddress2,
dr.BillingCity,
dr.BillingState,
dr.BillingZip,
dr.BillingCountry,
dr.PrimaryCommunicationContact,
dr.DefaultAuthorizedEnrollmentSigner,
dr.SecondaryCommunicationContact,
dr.AdditionalAuthorizedSigners,
COALESCE(ae.AggregatedEntityId, 'NULL') AS EntityId, -- Use aggregated EntityId or 'NULL'
dr.claimsManagementProPartnerAlias
FROM DistinctRows dr
LEFT JOIN AggregatedEntityIds ae ON dr.RowNum = ae.MaxRowNum
WHERE dr.RowNum = 1; -- Select only the distinct rows