from
甲,乙
where
甲.编号=乙.数值
group
by
甲.编号
单独求某一个编号的和,可以
select
甲.编号,sum(乙.数值)
from
甲,乙
where
甲.编号=乙.数值
and
甲.编号=xxx
create Table T(
id int,
a int,
b int
)
Insert into t values(1,1,1)
Insert into t values(2,2,1)
Insert into t values(3,3,1)
Insert into t values(4,4,1)
Insert into t values(5,5,1)
Insert into t values(6,6,1)
/*思路:
id可能不连续,所以先用id排序,产生一个记录号id2
然后按3条记录一组产生一个分组号G,接着按G分组求和,id取最大
*/
With CT
AS
(
Select max(id) As id,SUM(a) As A,SUM(B) As B
from
(
Select *,(id2+2)/3 As G
from (Select *,ROW_NUMBER() over(order by id) As id2 from T) S
)M Group by G
)
Update T Set a=CT.A-T.a,b=CT.B-T.b
From CT
where T.id=CT.id
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)