Can someone help out with calculating a total row at the bottom of this PIVOT table please?
select *, [Drug1] + [Drug2] + [Drug3] + [Drug4] + [Drug5] as [Total]
from
(Select [id], [drug], [Diagnosis]
from DrugDiagnosis
) as ptp
pivot
(count(id)
for drug
in ([Drug1], [Drug2], [Drug3], [Drug4], [Drug5])
) as PivotTable
I know I can do it with a UNION and have a separate query to calc the totals, but that will double the hit on the database.
I have found examples using ROLLUP and CUBE, but these are deprecated features so I don't want to use them.
Any other ideas, GROUPING SETS maybe?