[原创]group by和compute 的使用

it2022-05-05  129

GROUP BY子句有个缺点,就是返回的结果集中只有合计数据,而没有原始的详细记录。如果想在SQL SERVER中完成这项工作,可以使用COMPUTE BY子句。COMPTE生成合计作为附加的汇总列出现在结果集的最后。当与BY一起使用时,COMPUTE 子句在结果集内生成控制中断和分类汇总。

1.我们用group by 的时候:

select zongfenshu ,xuehao from aaa group by xuehao ,zongfenshu order by xuehao

语句运行结果:

2.我们用compute 的时候:

select xuehao,zongfenshu,dengji from aaa order by xuehao compute sum(zongfenshu)

语句运行结果:

3.我们用compute by 的时候:

select xuehao,zongfenshu,dengji from aaa order by xuehao compute sum(zongfenshu) by xuehao

 

语句运行结果:

转载于:https://www.cnblogs.com/crystal-guoguo/archive/2013/02/27/2934812.html


最新回复(0)