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.