How to Add A Second Inner Joined Table to SQL Server Pivot Query

Al C 41 Reputation points
2023-01-06T01:34:44.697+00:00

Good Evening,

I have been trying to add a second inner-joined table by the name of HS_SYS_ID_500A to the SQL Server query below. The join would be on health_sys_id.

SELECT * FROM
(
SELECT Value, health_sys_id,ffy, LineNumber = R.LINE_NUM_START+'-'+R.LINE_NUM_END
FROM Query1
INNER JOIN LINE_NUM_RANGE3 R ON LineNumber BETWEEN R.LINE_NUM_START AND LINE_NUM_END
WHERE (health_sys_id BETWEEN 'HSI00000008' AND 'HSI00001365') AND NOT (LineNumber ='862')

) t
PIVOT(
SUM(Value)
FOR ffy IN (
[2018],
[2019],
[2020],
[2021])
) AS pivot_table
ORDER BY health_sys_id, category

Thank you for taking the time to read and respond to this question.

Al

Developer technologies | Transact-SQL
SQL Server | Other
{count} votes

Accepted answer
  1. Anonymous
    2023-01-09T05:42:38.88+00:00

    Hi @Al C

    This time I just added a third table using an inner join.

    SELECT *   
    FROM (SELECT A.Value,A.health_sys_id,A.ffy,LineNumber = R.LINE_NUM_START +'-'+ R.LINE_NUM_END  
          FROM Query1 as A  
          INNER JOIN LINE_NUM_RANGE3 R ON LineNumber BETWEEN R.LINE_NUM_START AND R.LINE_NUM_END  
          INNER JOIN HS_SYS_ID_500A as B on A.health_sys_id = B.health_sys_id  
          WHERE (A.health_sys_id BETWEEN 'HSI00000008' AND 'HSI00001365') AND NOT (LineNumber ='862')  
         ) t  
    PIVOT(SUM(Value) FOR ffy IN ([2018],[2019],[2020],[2021])) AS pivot_table  
    ORDER BY health_sys_id, category;  
    

    I tried based on the three tables of data you provided, but nothing can be output. Because I don't know what the result you need the final output to look like.

    Best regards,
    Percy Tang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Anonymous
    2023-01-06T03:05:38.25+00:00

    Hi @Al C

    Based on your request, I tried to write the following query. Since there are some confusing places in your code, I made some modifications to these places. If you are still confused, you can continue to leave a message.

    ;With CTE as(  
      SELECT A.Value,A.health_sys_id,A.ffy,R.LINE_NUM_END - R.LINE_NUM_START as LineNumber  
      FROM Query1 as A  
      INNER JOIN LINE_NUM_RANGE3 as R ON R.LINE_NUM_END - R.LINE_NUM_START BETWEEN R.LINE_NUM_START AND R.LINE_NUM_END  
      INNER JOIN HS_SYS_ID_500A as B ON A.health_sys_id = B.health_sys_id)  
    SELECT * FROM  
    (select * from CTE WHERE health_sys_id BETWEEN 'HSI00000008' AND 'HSI00001365' AND LineNumber <> '862') as t  
    PIVOT(SUM(Value) FOR ffy IN ([2018],[2019],[2020],[2021])) AS pivot_table  
    ORDER BY health_sys_id, category  
    

    Best regards,
    Percy Tang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.