Is there any way to have a CASE SELECT Statement in a PIVOTED Column. My Code is as follows
SELECT PName, [RN], [HA], [LVN], [MSW], [SC]
FROM
(
Query
) src
pivot
(
max(Visits)
for Discipline in ([RN], [HA], [LVN], [MSW], [SC])
) piv
I am getting the output as follows
Pname RN HA LVN MSW SC
AA AG-2/W LO-1/W NA-1/W SK-2/W NO-2/MON
AA JL-2/W NULL NULL NULL NULL
Because there have been 2 RNs assigned to 1 PN I want to summarize the results only in 1 Row and select only 1 value to be displayed in the RN column so that the result is only as follows based on my condition.
Pname RN HA LVN MSW SC
AA JL-2/W LO-1/W NA-1/W SK-2/W NO-2/MON
You’ll have to remove the data AFTER the pivoting, because [RN] itself is subject to a MAX condition, i.e. it is itself a pivot column. Wrap it in a CTE, apply a RowNumber() function and partition correctly, then filter it for just 1 row per partition.