【发布时间】:2018-09-28 08:58:49
【问题描述】:
我有一个表格,其中包括月份、accountID 和一组应用程序分数。我想创建一个新列,为每个月的顶部、中间和底部 33% 的结果提供“高”、“中”或“低”。
如果我使用 rank(),我可以订购一个月或整个数据集的应用程序分数,但我不确定如何按月订购。另外,在我的 sql server percent_rank() 版本上不起作用。
select
AccountID
, ApplicationScore
, rank() over (order by applicationscore asc) as Rank
from Table
然后我知道我需要将 rank() 语句放在子查询中,然后使用 case 语句来应用“高”、“中”或“低”。
select
AccountID
, case when rank <= total/3 then 'low'
when rank > total/3 and rank <= (total/3)*2 then 'medium'
when rank > (total/3)*2 then 'high' end ApplicationScore
from (subquery) a
【问题讨论】:
-
您的预期输出是什么?想要每个月内的排名,还是整个数据集的排名?
-
@TimBiegeleisen 我希望我的表显示月份、帐户 ID、应用程序分数、排名。排名实际上是 MonthlyRank,它给出“低”、“中”或“高”排名,具体取决于它是在该月结果的前三分之一、中还是后三分之一
标签: sql sql-server case rank percentile