标签:
join 自连接有一个小小的应用,就是用于累计统计
1,创建实例代码
create table dbo.FinanceMonth (
MonthNum int , quantity int ) ;with cte as ( select 1 as MonthNum,100 as quantity union all select MonthNum+1,quantity+100 from cte where MonthNum<12 ) insert into dbo.FinanceMonth select * from cte
2,采用自链接方式计算累加和
select a.MonthNum,sum(b.quantity) as TotalQuantity from dbo.FinanceMonth a inner join dbo.FinanceMonth b on a.MonthNum>=b.MonthNum group by a.MonthNum order by a.MonthNum
标签:
原文地址:http://www.cnblogs.com/ljhdo/p/4532648.html