sql2000 top 为参数 怎么做

2024-12-28 01:14:31
推荐回答(2个)
回答1:

比如:

有一张横表A,表中有字段:镇局area,manager客户经理,custmer 客户,talk_no通话次数

每个镇下面有多个客户经理,每个客户经理管理多个客户,每个客户有各自的通话次数

那么如果想要取出每个客户经理下面通话次数前5个的客户,应该怎么写sql呢?
解:
select area 区域,manager 客户经理,custmer 客户,talk_no 通话次数
from (
select area,manager,customer,talk_no,
dense_rank() over (partition by manager order by talk_no desc) rank_no
) where rank_no <= 5;
帮顶,是Oracle的,找高手帮你转下

回答2:

/*
declare @num int
set @num =10
select top (@num) id from test
*/
declare @num int
set @num =10
DECLARE @sqlstr NVARCHAR(200)
SELECT @sqlstr = 'select top ('+CAST(@num AS VARCHAR(10)) + ') id from test '
EXEC(@sqlstr)