I have a table A with the following columns:
Status varchar
StateCode bit
OpportunityID int
so if i do: SELECT count(distinct opportunityID) from table A group by Status I get the count. Now in the same query I want to add another columns called CurrentCount and HistoricalCount. The current count is the count of distinct opportunityID for a particular Status and StateCode = 0 and the HistoricalCount is the count of distinct opportunityID for a particular Status and StateCode = 1. In other words Count = (CurrentCount + HistoricalCount ).
How do I accomplish this ??
Any ideas and suggestions are well appreciated !
You can use a
casein yourcountstatements so you only count whereStateCodehas the desired value.No, that will not be true if you have a
OpportunityIDthat has bothStateCodeas1and0.Ex:
Result: