[Quote=引用 4 楼 timeyh 的回复:]
意思是指按部门统计每一个部门的前500位。
[/Quote]
以下两种你选择你需要的一种。
--1
select t.* from tb t where 金额 in (select top 500 金额 from tb where 部门 = t.部门 order by 金额 desc)
--2
select m.* from
(
select 部门,编码,sum(数量) 数量,sum(金额) 金额 from tb group by 部门,编码
) m where 金额 in
(select top 500 * 金额 from
(
select 部门,编码,sum(数量) 数量,sum(金额) 金额 from tb group by 部门,编码
) n
where n.部门 = m.部门 order by n.金额 desc)