To achieve the desired result, we need to group rows based on all columns except EntityId and combine the EntityId values into a single string separated by ~ when there are multiple distinct values. If one of the EntityId values is NULL, it should be ignored unless all rows for that group have NULL.
Here’s how you can write the query in MS SQL:
Query Explanation:
- Grouping: We will group the rows by all columns except
EntityId.
- Combining
EntityId Values: Use STRING_AGG (or an equivalent method if your SQL Server version doesn't support STRING_AGG) to concatenate the EntityId values, ensuring NULL values are excluded.
- Handling NULLs: If all
EntityId values for a group are NULL, the result should be NULL.
SQL Query:
WITH CombinedEntityIds AS (
SELECT
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,
STRING_AGG(EntityId, '~') WITHIN GROUP (ORDER BY EntityId) AS EntityType
FROM #DistinctDataWithMaxRowNumber
WHERE EntityId IS NOT NULL -- Exclude NULL EntityIds during concatenation
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
)
SELECT
d.HistoricalDataRecordId,
d.TerminalId,
d.UserEmail,
d.CustomerName,
d.AbilityAccountId,
d.Type,
d.First,
d.Middle,
d.Name,
d.Suffix,
d.DoingBusinessAs,
d.Description,
d.Address1,
d.Address2,
d.City,
d.State,
d.Zip,
d.Country,
d.Phone,
d.EIN,
d.NPI,
d.PhoneExtension,
d.Email,
d.Fax,
d.BillingAddress1,
d.BillingAddress2,
d.BillingCity,
d.BillingState,
d.BillingZip,
d.BillingCountry,
d.PrimaryCommunicationContact,
d.DefaultAuthorizedEnrollmentSigner,
d.SecondaryCommunicationContact,
d.AdditionalAuthorizedSigners,
d.claimsManagementProPartnerAlias,
COALESCE(c.EntityType, NULL) AS EntityType
FROM #DistinctDataWithMaxRowNumber d
LEFT JOIN CombinedEntityIds c
ON
d.HistoricalDataRecordId = c.HistoricalDataRecordId AND
d.TerminalId = c.TerminalId AND
d.UserEmail = c.UserEmail AND
d.CustomerName = c.CustomerName AND
d.AbilityAccountId = c.AbilityAccountId AND
d.Type = c.Type AND
d.First = c.First AND
d.Middle = c.Middle AND
d.Name = c.Name AND
d.Suffix = c.Suffix AND
d.DoingBusinessAs = c.DoingBusinessAs AND
d.Description = c.Description AND
d.Address1 = c.Address1 AND
d.Address2 = c.Address2 AND
d.City = c.City AND
d.State = c.State AND
d.Zip = c.Zip AND
d.Country = c.Country AND
d.Phone = c.Phone AND
d.EIN = c.EIN AND
d.NPI = c.NPI AND
d.PhoneExtension = c.PhoneExtension AND
d.Email = c.Email AND
d.Fax = c.Fax AND
d.BillingAddress1 = c.BillingAddress1 AND
d.BillingAddress2 = c.BillingAddress2 AND
d.BillingCity = c.BillingCity AND
d.BillingState = c.BillingState AND
d.BillingZip = c.BillingZip AND
d.BillingCountry = c.BillingCountry AND
d.PrimaryCommunicationContact = c.PrimaryCommunicationContact AND
d.DefaultAuthorizedEnrollmentSigner = c.DefaultAuthorizedEnrollmentSigner AND
d.SecondaryCommunicationContact = c.SecondaryCommunicationContact AND
d.AdditionalAuthorizedSigners = c.AdditionalAuthorizedSigners AND
d.claimsManagementProPartnerAlias = c.claimsManagementProPartnerAlias
GROUP BY
d.HistoricalDataRecordId,
d.TerminalId,
d.UserEmail,
d.CustomerName,
d.AbilityAccountId,
d.Type,
d.First,
d.Middle,
d.Name,
d.Suffix,
d.DoingBusinessAs,
d.Description,
d.Address1,
d.Address2,
d.City,
d.State,
d.Zip,
d.Country,
d.Phone,
d.EIN,
d.NPI,
d.PhoneExtension,
d.Email,
d.Fax,
d.BillingAddress1,
d.BillingAddress2,
d.BillingCity,
d.BillingState,
d.BillingZip,
d.BillingCountry,
d.PrimaryCommunicationContact,
d.DefaultAuthorizedEnrollmentSigner,
d.SecondaryCommunicationContact,
d.AdditionalAuthorizedSigners,
d.claimsManagementProPartnerAlias,
c.EntityType;