-1

我有一个返回以下结果集的查询:

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 )
4

1 回答 1

4

您可以使用该PIVOT功能来执行此操作。如果您知道值,则可以使用静态数据透视表对列进行硬编码。为了在 中正确分组PIVOT,我包括了 arow_number()所以结果将包括每条记录,而不仅仅是max()per 的值patname

select patname, [HA], [MSW], [RN]
from 
(
  select patName, Disc,
    sname+' '+schedule new_value,
    row_number() over(partition by patname, disc order by disc) rowNum
  from <yourquery here>
) src
pivot
(
  max(new_value)
  for disc in ([HA], [MSW], [RN])
) piv

请参阅带有演示的 SQL Fiddle

如果DISC字段中有未知值,则可以使用动态 SQL 来透视数据:

DECLARE @cols AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX)

select @cols = STUFF((SELECT distinct ',' + QUOTENAME(disc) 
                    from yourquery
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @query = 'SELECT patname, ' + @cols + ' 
            from 
             (
               select patName, Disc,
                sname+'' ''+schedule new_value,
                row_number() over(partition by patname, disc order by disc) rowNum
              from <yourquery here>
            ) x
            pivot 
            (
                max(new_value)
                for Disc in (' + @cols + ')
            ) p '

exec(@query)

请参阅带有演示的 SQL Fiddle

如果您没有PIVOT可用的函数,则可以使用聚合函数和CASE语句来复制:

select patname,
  max(case when disc='HA' then new_value end) HA,
  max(case when disc='MSW' then new_value end) MSW,
  max(case when disc='RN' then new_value end) RN,
  rowNum
from
(
  select patName, Disc,
    sname+' '+schedule new_value,
    row_number() over(partition by patname, disc order by disc) rowNum
  from yourquery
) src
group by patname, rownum

请参阅带有演示的 SQL Fiddle

编辑,根据您发布的查询,您可能需要使用以下内容:

SELECT PatientName, [RN], [HA], [LVN], [MSW], [SC] 
FROM 
( 
   SELECT 
      (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 
  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 )
)src  
pivot
(
  max(new_value)
  for Discipline in ([RN], [HA], [LVN], [MSW], [SC])
) piv
于 2012-11-06T02:38:10.080 回答