日期:2014-05-17  浏览次数:20429 次

求一動態SQL語句?
表A:

partid orderid qty
0001 123 2500
0001 234 400
...
現在想得到的結果是:
partid orderid qty
0001 123,234 2900

急!在線等!

------解决方案--------------------
SQL code
select partid,orderid=stuff((select ','+orderid from tb where t.partid=partid for xml path('')),1,1''),qty=sum(qty)
from tb t
group by partid

------解决方案--------------------
SQL code
select partid,orderid=stuff((select ','+orderid from tb where t.partid=partid for xml path('')),1,1,''),qty=sum(qty)
from tb t
group by partid

------解决方案--------------------
SQL code

select partid,
       stuff((select ','+orderid from a where partid=t.partid for xml path('')),1,1,'') orderid,
       sum(qty) qty
from a t
group by partid

------解决方案--------------------
SQL code
CREATE function dbo.fn_date(@id as varchar(50))
returns varchar(1000)
as   
begin
declare @s varchar(1000)
set @s=''
select @s=@s+isnull(time1,'')+';'
from tb where partid=@id
return (@s)
end



select partid,dbo.fn_date(partid) as orderid,
from A
group by partid

----------------