sql多条件分类统计

timeyh 2010-11-20 10:59:32
我要统计一个表,按门店统计销售前500位;
表结构为:部门、编码、数量、金额;
如果是按编码统计可以这样:select top 500 编码,sum(数量),sum(金额) from diandan group by 编码 order by sum(金额)desc
如果统计部门和编码统计怎么写语句,
...全文
417 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
dawugui 2010-11-21
  • 打赏
  • 举报
回复
[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)
水族杰纶 2010-11-21
  • 打赏
  • 举报
回复
如果统计部门和编码统计怎么写语句,
没懂
timeyh 2010-11-21
  • 打赏
  • 举报
回复
语句怎么写。。请指教的。。。
永生天地 2010-11-21
  • 打赏
  • 举报
回复
需要用row_number进行排号
timeyh 2010-11-21
  • 打赏
  • 举报
回复
意思是指按部门统计每一个部门的前500位。
chen8410 2010-11-20
  • 打赏
  • 举报
回复
select top 500 部门 , 编码,sum(数量),sum(金额) 
from diandan
group by 部门 , 编码
order by sum(金额)desc
dawugui 2010-11-20
  • 打赏
  • 举报
回复
select top 500 部门 , 编码,sum(数量),sum(金额)
from diandan
group by 部门 , 编码
order by sum(金额)desc

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧