declare @a varchar(8000),@b int,@c intselect a.so_contract_id,(b.unit*b.price) as total,a.so_contract_date,a.delivery_date, c.prod_type,b.prod_id,0 gr,cast('' as varchar(8000)) value into #aaa 
from so_contract a,so_contract_prod b,product c
where a.so_contract_id=b.so_contract_id and b.prod_id=c.prod_id and (a.state=(select max(approver_step) from process_function,approve_process 
       where process_function.approver_id=approve_process.approver_id and process_function.table_name='so_contract'))
set @c=0
update #aaa set @a=case when @b=so_contract_id then @a else '' end+prod_type+',',
@c=case when @b=so_contract_id then @c else @c+1 end,@b=so_contract_id,gr=@c,value=@aselect max(so_contract_id) as so_contract_id,max(value) result into #bbb from #aaa group by gr
--select * from #bbb 
--select * from #aaa
select a.so_contract_id,(select sum(total) from #aaa  ) as total,a.so_contract_date,a.delivery_date,a.prod_id,b.result as prod_type
from #aaa a,#bbb b
where a.so_contract_id=b.so_contract_id and a.value=b.result
--select @d=@d+','+ch from #临时表
--select right(@d,len(@d)-1)drop table #aaa
drop table #bbb你去是一下吧。前天有人问过大力。我的问题也解决了。应该是你想要得吧!!!
解决了要给分呀