I have an insert query I'm currently using in a stored procedure that works as it should. It is as follows:
insert into tblAgentRank (AgtID, RankType, Rank, TimeFrame, RankValue)
select AgtID, 8, RANK() OVER (order by SUM(ColPrem*ModeValue) DESC) as Rank, 'Y', SUM(ColPrem*ModeValue)
from tblAppsInfo
where CompanyID in (select CompanyID from tblCompanyInfo
where DeptID = 7)
group by AgtID
order by Rank
This creates a total for each agent, and ranks them against their peers.
I need to create a similar statement that does the following calculations:
- If PolicyTypeID = 4, calculate SUM(ColPrem*ModeValue) * 0.07
- Else, calculate SUM((ColPrem*ModeValue) + (ExcessPrem * 0.07))
- Sum these two statements together for each agent, and then do the rank on the total.
I could easily do one of those, as demonstrated by the 1st query. My mental block is stemming from needing to do it on a case by case basis based on PolicyTypeID.