Software used: SQL Server 2014 Management Studio to query a standard 64-bit SQL Server.
I'm trying to create a query that generates a set of patient assessment results across multiple patients / assessments, and to put the results of each assessment on one row. This would suggest the use of pivoting of some sort.
Each assessment consists of a number of tests, each of which has a type, a name and a score. It is possible that we may want to add more tests to an assessment at some point, which would suggest the use of dynamic pivots.
At the moment, my query essentially looks like this:
SELECT TOP 20000
P.PatientId,
P.LastName,
P.FirstName,
FORMAT(P.DateOfBirth, 'dd/MM/yyyy') as 'DateOfBirth',
FORMAT(A.TreatmentDate, 'dd/MM/yyyy') as 'AssmentDate',
A.TestType,
A.TestName,
A.TestScore
FROM
Patient P
INNER JOIN
Assessment A ON (A.PatientSerialNumber = P.PatientSerialNumber)
INNER JOIN
AssessmentHeader AH ON (AH.AssessmentSerialNumber = A.AssessmentSerialNumber
AND AH.PatientSerialNumber = P.PatientSerialNumber)
WHERE
A.ValidEntryIndicator = 'Y'
AND AH.ValidEntryIndicator = 'Y'
ORDER BY
P.PatientId, T.TreatmentDate
My results essentially look like this (there are actually a lot more test types and names than shown here - these are just for illustration):
PatientId | LastName | FirstName | DateOfBirth | AssmentDate | TestType | TestName | TestScore |
---|---|---|---|---|---|---|---|
AB1234 | PATIENT | Test | 1/1/2000 | 1/1/2020 | Renal | Urgency | 0 |
AB1234 | PATIENT | Test | 1/1/2000 | 1/1/2020 | Renal | Retention | 1 |
AB1234 | PATIENT | Test | 1/1/2000 | 1/1/2020 | GI | Proctitis | 2 |
AB1234 | PATIENT | Test | 1/1/2000 | 1/1/2020 | GI | Diarrhea | 3 |
AB1234 | PATIENT | Test | 1/1/2000 | 6/6/2021 | Renal | Urgency | 1 |
AB1234 | PATIENT | Test | 1/1/2000 | 6/6/2021 | Renal | Retention | 0 |
AB1234 | PATIENT | Test | 1/1/2000 | 6/6/2021 | GI | Proctitis | 1 |
AB1234 | PATIENT | Test | 1/1/2000 | 6/6/2021 | GI | Diarrhea | 2 |
YZ6789 | PATIENT2 | Test2 | 1/1/1999 | 7/7/2020 | Renal | Urgency | 2 |
YZ6789 | PATIENT2 | Test2 | 1/1/1999 | 7/7/2020 | Renal | Retention | 5 |
YZ6789 | PATIENT2 | Test2 | 1/1/1999 | 7/7/2020 | GI | Proctitis | 3 |
YZ6789 | PATIENT2 | Test2 | 1/1/1999 | 7/7/2020 | GI | Diarrhea | 1 |
YZ6789 | PATIENT2 | Test2 | 1/1/1999 | 6/7/2021 | Renal | Urgency | 2 |
YZ6789 | PATIENT2 | Test2 | 1/1/1999 | 6/7/2021 | Renal | Retention | 9 |
YZ6789 | PATIENT2 | Test2 | 1/1/1999 | 6/7/2021 | GI | Proctitis | 4 |
YZ6789 | PATIENT2 | Test2 | 1/1/1999 | 6/7/2021 | GI | Diarrhea | 5 |
What I would like is this:
PatientId | LastName | FirstName | DateOfBirth | AssmentDate | Renal-Urgency | Renal-Retention | GI-Proctitis | GI-Diarrhea |
---|---|---|---|---|---|---|---|---|
AB1234 | PATIENT | Test | 1/1/2000 | 1/1/2020 | 0 | 1 | 2 | 3 |
AB1234 | PATIENT | Test | 1/1/2000 | 6/6/2021 | 1 | 0 | 1 | 2 |
YZ6789 | PATIENT2 | Test2 | 1/1/1999 | 7/7/2020 | 2 | 5 | 3 | 1 |
YZ6789 | PATIENT2 | Test2 | 1/1/1999 | 6/7/2021 | 2 | 9 | 4 | 5 |
I've tried to follow various online resources and SO question/answers (this one looked the most promising), but I just can't seem to get the methods shown in these to work with my query (frustratingly)
Can anyone help me out?