比如先想实现一个简单的三条记录(或者更多条)count price type1 10 1 2 3 13 4 14 5 15 2 26 1 2我想输出 最后的结果表达式 type1: (1*10)+(2*3)+(3*4)+(4*5)=48type2: (5*2)+(6*1)=16这个要怎么做才能分组显示出想要的字符串呢------------------------
CREATE TABLE tb( pid int, id int, [type] int)insert tbselect 1, 10, 1 union allselect 2, 3, 1 union allselect 3, 4, 1 union allselect 4, 5, 1 union allselect 5, 2, 2 union allselect 6, 1, 2go
-----------------------------
select ltrim(SUM(pid*id)) from tb group by type
select 'type'+ltrim([type])+stuff( (select '+('+ltrim(pid)+'*'+ltrim(id)+')' from tb where [type]=ta.[type] for xml path('')),1,1,'')+'='+ltrim(SUM(pid*id)) as col from tb as ta group by [type]