日期:2014-05-16 浏览次数:20584 次
楼主你是想按照某个列汇总另外一个列,同时查询其他的列吧
推荐窗口函数,用起来贼爽
create table #t
(
column1 int,
column2 int,
column3 int,
column4 int
)
insert into #t values (1,RAND()*100,RAND()*100,RAND()*100)
insert into #t values (2,RAND()*100,RAND()*100,RAND()*100)
insert into #t values (2,RAND()*100,RAND()*100,RAND()*100)
insert into #t values (3,RAND()*100,RAND()*100,RAND()*100)
insert into #t values (4,RAND()*100,RAND()*100,RAND()*100)
select column1,SUM(column2)over (partition by column1),column3,column4 from #t
select orderid,custid,val,
SUM(val) over() as totalValue, --不分区 sum(val)为val的总和
SUM(val) over(partition by custid) as custtotalvalue --partition by custid 按照custid分区, sum(val)为一个custid的总和
from Sales.OrderValues
--partition by custid 按照custid分区