SQL Server 2005第_行编号()超过的问题(订货人…)
这个问题与我之前问的一个问题有关。。。请参阅“SQLServer2005如何在使用LIKE运算符时对记录集进行排序” 以下作品SQL Server 2005第_行编号()超过的问题(订货人…),sql,sql-server-2005,tsql,Sql,Sql Server 2005,Tsql,这个问题与我之前问的一个问题有关。。。请参阅“SQLServer2005如何在使用LIKE运算符时对记录集进行排序” 以下作品 with xx as ( select case when mycol = 'finance' then 1 when mycol like 'finance%' then 2 when mycol like '%finance%' then 3 end as rnk, * from MyTable where my
with xx as
(
select case
when mycol = 'finance' then 1
when mycol like 'finance%' then 2
when mycol like '%finance%' then 3
end as rnk,
*
from MyTable
where mycol like '%finance%'
)
select * from xx
order by xx.rnk, xx.mycol;
但我真正想做的是,当我使用服务器翻阅记录时
WITH xx AS
(
select case
when t.term = 'finance' then 1
when t.term like 'finance%' then 2
when t.term like '%finance%' then 3
end as rnk,
*,
row_number() over (order by rnk, t.term) as rownumber
from tblTerms t
where t.term like '%finance%'
)
select * from xx
where rownumber between 11 and 20 -- page #2
order by xx.rnk, xx.mycol;
我收到一个错误“无效列名'rnk'”
有人对如何解决这个问题有什么想法吗
非常感谢,
标记问题出在公共表表达式(CTE)定义中。不能在定义列别名的同一SELECT子句中引用
rnk
。您可以尝试以下方法:
WITH xx AS
(
select case
when t.term = 'finance' then 1
when t.term like 'finance%' then 2
when t.term like '%finance%' then 3
end as rnk,
*
from tblTerms t
where t.term like '%finance%'
),
yy AS
(
SELECT *,
row_number() over (order by rnk, term) as rownumber
FROM xx
)
select * from yy
where rownumber between 11 and 20 -- page #2
order by yy.rnk, yy.mycol;
问题出在公共表表达式(CTE)定义中。不能在定义列别名的同一SELECT子句中引用
rnk
。您可以尝试以下方法:
WITH xx AS
(
select case
when t.term = 'finance' then 1
when t.term like 'finance%' then 2
when t.term like '%finance%' then 3
end as rnk,
*
from tblTerms t
where t.term like '%finance%'
),
yy AS
(
SELECT *,
row_number() over (order by rnk, term) as rownumber
FROM xx
)
select * from yy
where rownumber between 11 and 20 -- page #2
order by yy.rnk, yy.mycol;
谢谢你,伙计。我复制并粘贴了你的答案到我的查询窗口,仍然得到错误消息。。。列名“rnk”无效。因此,我从with xx语句中删除了行_number(),并从上一个order by中删除了yy.mycol,从而修复了它。。。现在我得到了我想要的结果。非常感谢@马克,对不起,弄错了。你修好了。我会为其他观众更正的。谢谢各位。我复制并粘贴了你的答案到我的查询窗口,仍然得到错误消息。。。列名“rnk”无效。因此,我从with xx语句中删除了行_number(),并从上一个order by中删除了yy.mycol,从而修复了它。。。现在我得到了我想要的结果。非常感谢@马克,对不起,弄错了。你修好了。我将为其他观众更正它。