请教一条有难度的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
[解决办法]
if object_id('[tb]') is not null drop table [tb]gocreate 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 allselect 400,'乐斯本','208L','00240786','乐斯本','标签' union allselect 400,'乐斯本','208L','00241150','乐斯本','瓶子' union allselect 400,'乐斯本','208L','00341260','乐斯本','卷膜' union allselect 401,'乐斯本2','218L','00341261','乐斯本2','卷膜'goselect ProductCode,ProductName,Type,Msg=stuff((select ','+PackCode+PackName+PackType from tb where ProductCode=t.ProductCode for xml path('')),1,1,'')from tb tgroup by ProductCode,ProductName,Type/**ProductCode ProductName Type Msg----------- ----------- ---- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------400 乐斯本 208L 00240781乐斯本大桶,00240786乐斯本标签,00241150乐斯本瓶子,00341260乐斯本卷膜401 乐斯本2 218L 00341261乐斯本2卷膜(2 行受影响)**/
[解决办法]
select ProductCode ,ProductName,Type,Msg=stuff((select ','+(PackCode+','+PackName+','+PackType) from @tb t where ProductCode =tv.ProductCode for xml path('')), 1, 1, '') from @tb as tvgroup by ProductCode
[解决办法]
create table test1(ProductCode varchar(20), ProductName varchar(20),Type varchar(20), PackCode varchar(20), PackName varchar(20), PackType varchar(20))--------------------------------insert into test1 values('400', '乐斯本', '208L', '00240781', '乐斯本', '大桶')insert into test1 values('400', '乐斯本', '208L', '00240786', '乐斯本', '标签')insert into test1 values('400', '乐斯本', '208L', '00241150', '乐斯本', '瓶子')insert into test1 values('400', '乐斯本', '208L', '00341260', '乐斯本', '卷膜')insert into test1 values('401', '乐斯本2','218L', '00341261', '乐斯本2','卷膜') select ProductCode,ProductName,Type, Msg=STUFF((select ','+PackCode+PackName+PackType from test1where a.ProductCode =ProductCode and a.ProductName =ProductName and a.Type =Type for xml path('')),1,1,'') from test1 agroup by ProductCode,ProductName,Typedrop table test1ProductCode,ProductName,Type, Msg400 乐斯本 208L 00240781乐斯本大桶,00240786乐斯本标签,00241150乐斯本瓶子,00341260乐斯本卷膜401 乐斯本2 218L 00341261乐斯本2卷膜