I have a couple SQL Server 2012 queries working but cannot figure out how to combine them into a single result set. I would like to see [Total Claims] and [Reversed Claims] as adjacent columns in the same result set. Is this possible?
select [Date], DATENAME(weekday, [Date]) as [Day], [Total Claims]
from (Select [Date], count(*) as [Total claims] from ClaimHistoryView group by [Date] )
as CountByDay
order by [Date] desc
select [Date], DATENAME(weekday, [Date]) as [Day], [Reversed Claims]
from (Select [Date], count(*) as [Reversed Claims] from ClaimHistoryView where status = 2 group by [Date] ) as CountByDay
order by [Date] desc
The trick is to combine the two queries using a ‘union all’ with another column indicating where the data comes from and then doing a selective pivot. Hmm. complicated explanation (sorry, german)
This should do the trick