日期:2014-05-18  浏览次数:20581 次

请教一条有难度的sql语句
ProductCode ProductName Type PackCode PackName PackType
--------------------------------
400 乐斯本 208L 00240781 乐斯本 大桶
400 乐斯本 208L 00240786 乐斯本 标签
400 乐斯本 208L 00241150 乐斯本 瓶子
400 乐斯本 208L 00341260 乐斯本 卷膜
401 乐斯本2 218L 00341261 乐斯本2 卷膜  

数据如上,要求查询后出来的结果是:
ProductCode ProductName Type Msg
--------------------------------
400 乐斯本 208L 00240781乐斯本大桶,00240786乐斯本标签,00241150乐斯本瓶子,00341260乐斯本卷膜
401 乐斯本2 218L 00341261乐斯本2卷膜 



------解决方案--------------------
http://topic.csdn.net/u/20080612/22/c850499f-bce3-4877-82d5-af2357857872.html
------解决方案--------------------
SQL code
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([ProductCode] int,[ProductName] varchar(7),[Type] varchar(4),[PackCode] varchar(8),[PackName] varchar(7),[PackType] varchar(4))
insert [tb]
select 400,'乐斯本','208L','00240781','乐斯本','大桶' union all
select 400,'乐斯本','208L','00240786','乐斯本','标签' union all
select 400,'乐斯本','208L','00241150','乐斯本','瓶子' union all
select 400,'乐斯本','208L','00341260','乐斯本','卷膜' union all
select 401,'乐斯本2','218L','00341261','乐斯本2','卷膜'
go

select ProductCode,ProductName,Type,
Msg=stuff((select ','+PackCode+PackName+PackType from tb where ProductCode=t.ProductCode for xml path('')),1,1,'')
from tb t
group by ProductCode,ProductName,Type

/**
ProductCode ProductName Type Msg
----------- ----------- ---- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------