I'm trying to do pretty much the same thing as these questions:
How to concatenate text from multiple rows into a single text string in SQL server?
Simulating group_concat MySQL function in Microsoft SQL Server 2005?
I believe I'm working in SQL Server 2005.
But I need to either, A.) Do it with multiple tables, selecting about 12 columns and using roughly 8 inner joins
or
B.) Do it with a Query that already exists with the multiple tables (using roughly 8 inner joins)
Here is the current Query:
SELECT Claim.ClaimID, ClaimStatus.ClaimStatus, Claim.Claimant, Claim.ClaimNumber, ClaimType.ClaimType, Facility.FacilityName, StateOrProvince.State,
MedicalPractitioner.FullName, Claim.ClaimOccurrenceDate, Patient.Allegation, Injury.InjuryDescription AS Injury, GetClaimTotals_vw.[Reserve Total],
ISNULL(GetClaimTotals_vw.[Expense Total], '$0') AS [Paid Total]
FROM StateOrProvince INNER JOIN
MedicalPractitioner INNER JOIN
Injury INNER JOIN
Patient ON Injury.InjuryID = Patient.Injury INNER JOIN
ClaimStatus INNER JOIN
ClaimType INNER JOIN
Facility INNER JOIN
Claim ON Facility.FacilityID = Claim.Facility INNER JOIN
GetClaimTotals_vw ON Claim.ClaimID = GetClaimTotals_vw.ClaimID ON ClaimType.ClaimTypeID = Claim.ClaimType ON ClaimStatus.ClaimStatusID = Claim.ClaimStatus ON
Patient.PatientID = Claim.Patient AND Claim.ClaimID = Patient.ClaimNumber INNER JOIN
ClaimInsureds ON Claim.ClaimID = ClaimInsureds.ClaimNumber ON MedicalPractitioner.PractitionerID = ClaimInsureds.MedicalPractitioner ON
StateOrProvince.StateOrProvinceID = Facility.StateProvince
ORDER BY ClaimStatus.ClaimStatus
Query I tried, but it just puts every single name for each Case/Claim and still repeats Cases/Claims
SELECT ClaimNumber, Stuff(
(SELECT N', ' + FullName FROM QueryName_1 FOR XML PATH(''),TYPE)
.value('text()[1]','nvarchar(max)'),1,2,N'')
FROM QueryName_1
Result:
Claim# | Status | MP(s)
A1002. Open. Alex Amond, John Doe, Richard Cool, Jane Smith
A1002. Open. Alex Amond, John Doe, Richard Cool, Jane Smith
A1006. Pending. Alex Amond, John Doe, Richard Cool, Jane Smith
A1008. Closed. Alex Amond, John Doe, Richard Cool, Jane Smith
Etc.
The result is like 100-200 rows of data.
Specifically, I need to Concat on average two names that are related by an ID in another Table.
Here is an example (I'll use Case# and Lawyers) snapshot of what is currently being output with the current Query:
Claim# | Status | MP(s) | Etc. | |....|
A1002. Open. John Doe
A1002. Open. Alex Almond
A1006. Pending. Richard Cool
A1008. Closed. Jane Smith
Etc.
What I need:
Claim# | Status | MP(s). | Etc. |. | ... |
A1002. Open. John Doe, Alex Almond
A1006. Pending. Richard Cool
A1008. Closed. Jane Smith
Etc.
Thanks.