-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、 を含めたので、結果には ごとの値row_number()だけでなく各レコードが含まれます。max()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 に答える