查询
select
territory, new,
rank() over (order by new ASC) as rank_1
case
when rank_1 <= (select count(new)/3 from ##final) then 'low'
when rank_1 <= (select (count(new)/3)*2 from ##final) then 'medium'
when rank_1 <= (select count(new) from ##final) then 'high'
else 'error'
end
from
##final
order by
territory
表
territory new
6310101 941
6310102 186
6310103 458
6310103 458
6310104 109
6310105 113
6310106 305
6310107 189
6310108 184
6310109 737
6310110 862
6310110 862
6310201 1079
6310202 236
6310203 1529
错误讯息:
Msg 156,Level 15,State 1,Line 3
附近的语法不正确
关键字&#39; case&#39;附近的语法不正确 Msg 156,Level 15,State 1,Line 4
关键字&#39;然后&#39;附近的语法不正确 消息156,15级,状态1,行5 关键字&#39;然后&#39;附近的语法不正确 Msg 156,Level 15,State 1,Line 6
关键字&#39;然后&#39;。
答案 0 :(得分:2)
您不能直接引用此列,需要子查询或CTE:
WITH CTE AS
(
SELECT territory, new,
RANK() Over (Order By new ASC) as rank_1
FROM ##final
)
SELECT territory, new, 'error' =
case
when rank_1 <= (select count(new)/3 from ##final) then 'low'
when rank_1 <= (select count(new)/3 from ##final) then 'medium'
when rank_1 <= (select count(new)/3 from ##final) then 'high'
else 'error' end
FROM CTE
ORDER BY territory