次の結果セットを返すクエリがあります。
patName SName DISC SCHEDULE
JM AA HA 2 per Week
JM MAC MSW 1 per Month
JM ANG RN 1 per Week
JM JON RN 1 per Week
JM LRH RN 1 per Week
次のように結果を表示する方法はありますかPIVOT
PATNAME HA MSW RN
JM AA 2/Week MAC 1/Month ANG 1/week
JON 1/Week
LRH 1/Week
編集、私のクエリは次のとおりです。
SELECT PatientName, [RN], [HA], [LVN], [MSW], [SC]
FROM
(
SELECT DISTINCT (tblPatient.FirstName +' '+ tblPatient.LastName) As PatientName,
(tblStaff.StaffFirstName+' '+tblStaff.StaffLastName) As StaffName,
(tblStaffDiscipline.Discipline)As Discipline,
CAST(tblFrequencyOfVisit.NoOfVisit As Varchar)+' per '
+ REPLACE (tblFrequencyOfVisit.Period,'/','') AS Visits new_value,
row_number() over(partition by PatientName, Discipline order by Discipline) rowNum
FROM tblPatient INNER JOIN
tblFrequencyOfVisit
ON tblPatient.PatientId = tblFrequencyOfVisit.PatientId
INNER JOIN tblStaffAssignment
ON tblPatient.PatientId = tblStaffAssignment.PatientId
AND tblFrequencyOfVisit.PatientId = tblStaffAssignment.PatientId
INNER JOIN tblStaffDiscipline
ON tblFrequencyOfVisit.DisciplineId = tblStaffDiscipline.DisciplineId
INNER JOIN tblStaff
ON tblStaffAssignment.StaffId = tblStaff.StaffId
AND tblStaffDiscipline.Discipline = tblStaff.StaffDisciplane
)src
pivot
(
max(new_value)
for Discipline in ([RN], [HA], [LVN], [MSW], [SC])
) piv
WHERE
tblpatient.PatientId = '138'
AND NOT tblPatient.SOC IS NULL
AND tblPatient.EOC IS NULL
AND tblPatient.Hospiceid = '1'
AND tblFrequencyofVisit.FromDate =
(Select MAX(FROMDATE)
From tblfrequencyofVisit
Where tblFrequencyOfVisit.PatientId = tblPatient.PatientId
AND tblFrequencyofVisit.Disciplineid = tblStaffDiscipline.Disciplineid )