【问题标题】:Setting up a RANK() on a query在查询上设置 RANK()
【发布时间】:2014-08-21 15:54:49
【问题描述】:

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

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() 列的最简单方法是什么?在将这些结果写入我的最终表之前,我是否需要将它们移植到临时表中?

【问题讨论】:

  • 最简单的方法是在您的Select 中添加一个Rank() 列...但我不知道您要完成什么。
  • 这个问题是我试图根据我正在创建的列进行排名。 SQL 似乎不太喜欢。

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


【解决方案1】:

将您当前的查询设为 CTE:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-11
    • 2015-12-07
    • 2022-10-13
    • 2016-05-14
    • 2012-03-03
    • 2011-10-29
    • 2014-02-18
    相关资源
    最近更新 更多