Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8788693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:12:05+00:00 2026-06-13T22:12:05+00:00

I have a query that returns the following result set: patName SName DISC SCHEDULE

  • 0

I have a query that returns the following result set:

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

Is there any way I could display the result as follows using PIVOT

PATNAME    HA           MSW             RN 
  JM    AA 2/Week   MAC 1/Month    ANG 1/week 
                                   JON 1/Week 
                                   LRH 1/Week

Edit, my query is as follows:

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 )
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T22:12:06+00:00Added an answer on June 13, 2026 at 10:12 pm

    You can use the PIVOT function to perform this. If you have known values, then you can hard-code the columns using a static pivot. To get the grouping correct in the PIVOT, I included a row_number() so the result will include each record not just the max() value 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
    

    See SQL Fiddle with Demo

    If you have unknown values in the DISC field, then you can use dynamic SQL to pivot the data:

    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)
    

    See SQL Fiddle with Demo

    If you do not have a PIVOT function available, then this can be replicated using an aggregate function and a CASE statement:

    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
    

    See SQL Fiddle with Demo

    Edit, based on your query that you posted it looks like you might need to use something like this:

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a query that returns a result set similar to the one below:
I have a query that returns data in the following format: id | name
Possible Duplicate: mysql count into PHP variable I have the following query that returns
I have the following schema, and would like to do a query that returns
I have a query which returns the result set as follows: Col1 A B
I currently have a query that returns results based on a dynamic set of
I have the following query that returns test questions, possible answers to those questions
I have this query that returns the results by ordering it by focus.name ASC.
I have a query that returns me around 6 million rows, which is too
I have a query that returns Properties for Rent / Sale on their respective

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.