Sql 在查询上设置秩()

Sql 在查询上设置秩(),sql,sql-server,sql-server-2005,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 A

我有下面的函数查询,用于为竞赛提取数据

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

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

将您当前的查询转换为CTE:

with t as (
      <your query here>
     )
select rank() over (order by is_cp desc) as rank, t.*
from t;
带t作为(
)
选择(order by is_cp desc)上方的秩()作为秩,t*
从t;

根据您如何定义等级以及它如何处理关系,您可能更喜欢
row\u number()
densite\u rank()

最简单的方法就是在
选择中添加
rank()
列。。。但我不知道你想做什么。问题是我试图根据我正在创建的专栏进行排名。SQL似乎不太喜欢。
with t as (
      <your query here>
     )
select rank() over (order by is_cp desc) as rank, t.*
from t;