我在SQL服务器中有一个如下所示的表:
Name Value
a 1
b 23
c 15
d 57
e 98
我需要将其转换为:
New Name Value
a-c 39 (a+b+c)
d-e 155 (d+e)
答案 0 :(得分:1)
假设您要按3分组,请尝试:
select min([Name]) + '-' + max([Name]) [New Name], sum([Value]) [Value]
from
(select [Name], [Value], row_number() over (order by [Name]) rn
from myTable) sq
group by floor((rn-1)/3)