在查询上设置RANK()

时间:2014-08-21 15:54:49

标签: sql sql-server sql-server-2005

我有以下功能查询为比赛提取数据。

select top 10 AgtInfo.AgtID as AgentID
   ,AgtFN + ' ' + Left(AgtLN, 1) as Name
   ,CAST(ROUND(SUM(case when AppsStatusType IN ('IS', 'CP') then
        (case when AppsInfo.PolicyTypeID in (105, 139) then 
            (case when AppsInfo.AppsEntryDate between '2014-09-01' AND '2014-09-30' then (((ColPrem * ModeValue) * 0.07 + (cast(ExcessPrem as decimal(18,2)) * 0.07)) * 2)
            else ((ColPrem * ModeValue) * 0.07 + (cast(ExcessPrem as decimal(18,2)) * 0.07)) end)
        else (case  when AppsInfo.AppsEntryDate between '2014-09-01' AND '2014-09-30' then (((ColPrem * ModeValue) + (cast(ExcessPrem as decimal(18,2)) * 0.07)) * 2)
            else((ColPrem * ModeValue) + (cast(ExcessPrem as decimal(18,2)) * 0.07)) end) end)
    else 0 end),2) as MONEY) as IS_CP
   ,CAST(ROUND(SUM(case when AppsStatusType = 'PD' then 
        (case when AppsInfo.PolicyTypeID in (105, 139) then 
            (case when AppsInfo.AppsEntryDate between '2014-09-01' AND '2014-09-30' then (((ColPrem * ModeValue) * 0.07 + (cast(ExcessPrem as decimal(18,2)) * 0.07)) * 2)
            else ((ColPrem * ModeValue) * 0.07 + (cast(ExcessPrem as decimal(18,2)) * 0.07)) end)
        else (case  when AppsInfo.AppsEntryDate between '2014-09-01' AND '2014-09-30' then (((ColPrem * ModeValue) + (cast(ExcessPrem as decimal(18,2)) * 0.07)) * 2)
            else((ColPrem * ModeValue) + (cast(ExcessPrem as decimal(18,2)) * 0.07)) end) end)
    else 0 end),2) as MONEY) as PD
   ,(case when SUM(case when AppsStatusType IN ('IS', 'CP') then ColPrem else 0 end) >= 10000 then 'Y' else 'N' end) as Qualified
   ,LEFT(GETDATE(), 11) as Date
from TblAppsInfo AppsInfo
inner join TblAgentInfo AgtInfo on AppsInfo.AgtID = AgtInfo.AgtID
inner join TblApplicationStatus_L AppsStatus ON AppsInfo.AppsStatusID = AppsStatus.AppsStatusID
inner join TblClientInfo ClientInfo ON AppsInfo.ClientID = ClientInfo.ClientID
inner join TblCompanyInfo CompInfo ON AppsInfo.CompanyID = CompInfo.CompanyID
inner join TblPolicyTypes_L PolTypes ON AppsInfo.PolicyTypeID = PolTypes.PolicyTypeID
inner join TblDepartment Dept ON CompInfo.DeptID = Dept.DeptID
where AppsInfo.AppsEntryDate >= '2014-07-01'
AND AppsInfo.AppsEntryDate < '2015-01-01'
AND Dept.DeptName = 'life'
group by AgtInfo.AgtID, AgtFN, AgtLN
order by IS_CP DESC, PD DESC

这会取回以下数据:

AgtID   Name        IS_CP       PD      Qualified   Date
---------------------------------------------------------------
7457    DIANE O     23800.00    6205.76 Y           Aug 21 2014
1137    PAULINE W   7000.00     1604.72 Y           Aug 21 2014
6085    AARON H     3990.00     1486.80 N           Aug 21 2014
8662    LINDSEY H   1578.48     0.00    N           Aug 21 2014
7653    AMBERLY B   1461.20     0.00    N           Aug 21 2014
8733    ANTHONY K   1454.04     339.00  N           Aug 21 2014
7670    TYLER T     1167.20     0.00    N           Aug 21 2014
1344    DANIEL V    990.72      0.00    N           Aug 21 2014
88      JERI W      919.08      0.00    N           Aug 21 2014
7781    CHRISTINE G 826.50      0.00    N           Aug 21 2014

在此数据集中定义RANK()列的最简单方法是什么?在将这些结果写入我的最终表之前,我是否需要将这些结果移植到临时表中?

1 个答案:

答案 0 :(得分:2)

将您当前的查询设为CTE:

with t as (
      <your query here>
     )
select rank() over (order by is_cp desc) as rank, t.*
from t;

根据您定义排名的方式及其处理关系的方式,您可能更喜欢row_number()dense_rank()