首先创建一个表,插入响应的数据
CREATE TABLE [dbo].[T_Temp] (
[styleno] [varchar] (20) COLLATE Chinese_PRC_CI_AS NULL ,
[colorname] [varchar] (20) COLLATE Chinese_PRC_CI_AS NULL ,
[season] [varchar] (20) COLLATE Chinese_PRC_CI_AS NULL ,
[price] [money] NULL ,
[size] [varchar] (5) COLLATE Chinese_PRC_CI_AS NULL ,
[qty] [smallint] NULL ,
[sizegroup] [varchar] (5) COLLATE Chinese_PRC_CI_AS NULL 

insert t_temp(styleno,colorname,season,price,size,qty,sizegroup) values ('S6MF01001','黑','秋季',699,46,1,'A')
insert t_temp(styleno,colorname,season,price,size,qty,sizegroup) values ('S6MF01001','黑','秋季',699,48,3,'A')
insert t_temp(styleno,colorname,season,price,size,qty,sizegroup) values ('S6MF01001','黑','秋季',699,50,4,'A')
insert t_temp(styleno,colorname,season,price,size,qty,sizegroup) values ('S6MF01001','黑','秋季',699,52,3,'A')
insert t_temp(styleno,colorname,season,price,size,qty,sizegroup) values ('S6MF01001','黑','秋季',699,54,2,'A')
insert t_temp(styleno,colorname,season,price,size,qty,sizegroup) values ('S6MF01002','军绿','秋季',799,48,2,'A')
insert t_temp(styleno,colorname,season,price,size,qty,sizegroup) values ('S6MF01002','军绿','秋季',799,50,3,'A')
insert t_temp(styleno,colorname,season,price,size,qty,sizegroup) values ('S6MF01002','军绿','秋季',799,52,3,'A')
insert t_temp(styleno,colorname,season,price,size,qty,sizegroup) values ('S6MF01002','军绿','秋季',799,54,2,'A')
insert t_temp(styleno,colorname,season,price,size,qty,sizegroup) values ('S6MF01002','深蓝','秋季',799,48,2,'A')
insert t_temp(styleno,colorname,season,price,size,qty,sizegroup) values ('S6MF01002','深蓝','秋季',799,50,3,'A')
insert t_temp(styleno,colorname,season,price,size,qty,sizegroup) values ('S6MF01002','深蓝','秋季',799,52,3,'A')
insert t_temp(styleno,colorname,season,price,size,qty,sizegroup) values ('S6MF01002','深蓝','秋季',799,54,2,'A')
insert t_temp(styleno,colorname,season,price,size,qty,sizegroup) values ('S6MF02001','黑蓝','秋季',599,29,2,'B')
insert t_temp(styleno,colorname,season,price,size,qty,sizegroup) values ('S6MF02001','黑蓝','秋季',599,30,3,'B')
insert t_temp(styleno,colorname,season,price,size,qty,sizegroup) values ('S6MF02001','黑蓝','秋季',599,32,3,'B')
insert t_temp(styleno,colorname,season,price,size,qty,sizegroup) values ('S6MF02001','黑蓝','秋季',599,34,2,'B')
insert t_temp(styleno,colorname,season,price,size,qty,sizegroup) values ('S6MF02001','黑蓝','秋季',599,36,1,'B')
insert t_temp(styleno,colorname,season,price,size,qty,sizegroup) values ('S6MF02002','黑蓝','秋季',599,29,1,'B')
insert t_temp(styleno,colorname,season,price,size,qty,sizegroup) values ('S6MF02002','黑蓝','秋季',599,30,2,'B')
insert t_temp(styleno,colorname,season,price,size,qty,sizegroup) values ('S6MF02002','黑蓝','秋季',599,32,3,'B')
insert t_temp(styleno,colorname,season,price,size,qty,sizegroup) values ('S6MF02002','黑蓝','秋季',599,34,2,'B')
insert t_temp(styleno,colorname,season,price,size,qty,sizegroup) values ('S6MF02002','黑蓝','秋季',599,36,1,'B')
insert t_temp(styleno,colorname,season,price,size,qty,sizegroup) values ('S6MW24001','橙','冬季',159,29,5,'E')
insert t_temp(styleno,colorname,season,price,size,qty,sizegroup) values ('S6MW24001','黑','冬季',159,29,5,'E')
insert t_temp(styleno,colorname,season,price,size,qty,sizegroup) values ('S6MW24001','浅花灰','冬季',159,29,5,'E')
insert t_temp(styleno,colorname,season,price,size,qty,sizegroup) values ('S6MW25001','橙','冬季',159,29,5,'E')对T_Temp做操作
Select SizeGroup,Size From T_Temp Group By SizeGroup,Size
A 46
A 48
A 50
A 52
A 54
B 29
B 30
B 32
B 34
B 36
E 29
我想要的一个结果是
A 46 48 50 52 54
B 29 30 32 34 36
E 29第2个查询
Select StyleNO,ColorName,SeaSon,Price,SizeGroup,Size,Qty from T_Temp order by StyleNO,ColorName,SizeGroup,Size
S6MF01001 黑 秋季 699 A 46 1
S6MF01001 黑 秋季 699 A 48 3
S6MF01001 黑 秋季 699 A 50 4
S6MF01001 黑 秋季 699 A 52 3
S6MF01001 黑 秋季 699 A 54 2
S6MF01002 军绿 秋季 799 A 48 2
S6MF01002 军绿 秋季 799 A 50 3
S6MF01002 军绿 秋季 799 A 52 3
S6MF01002 军绿 秋季 799 A 54 2
S6MF01002 深蓝 秋季 799 A 48 2
S6MF01002 深蓝 秋季 799 A 50 3
S6MF01002 深蓝 秋季 799 A 52 3
S6MF01002 深蓝 秋季 799 A 54 2
S6MF02001 黑蓝 秋季 599 B 29 2
S6MF02001 黑蓝 秋季 599 B 30 3
S6MF02001 黑蓝 秋季 599 B 32 3
S6MF02001 黑蓝 秋季 599 B 34 2
S6MF02001 黑蓝 秋季 599 B 36 1
S6MF02002 黑蓝 秋季 599 B 29 1
S6MF02002 黑蓝 秋季 599 B 30 2
S6MF02002 黑蓝 秋季 599 B 32 3
S6MF02002 黑蓝 秋季 599 B 34 2
S6MF02002 黑蓝 秋季 599 B 36 1S6MW24001 橙 冬季 159 E 29 5
S6MW24001 黑 冬季 159 E 29 5
S6MW24001 浅花灰 冬季 159 E 29 5
S6MW25001 橙 冬季 159 E 29 5我想要的另外一个结果
S6MF01001 黑 秋季 699 A 1 3 4 3 2 13
S6MF01002 军绿 秋季 799 A 0 2 3 3 2 10
S6MF01002 深蓝 秋季 799 A 0 2 3 3 2 10
S6MF02001 黑蓝 秋季 599 B 2 3 3 2 1 11
S6MF02002 黑蓝 秋季 599 B 1 2 3 2 1 9
S6MW24001 橙 冬季 159 E 5 0 0 0 0 5
S6MW24001 黑 冬季 159 E 5 0 0 0 0 5
S6MW24001 浅花灰 冬季 159 E 5 0 0 0 0 5
S6MW25001 橙 冬季 159 E 5 0 0 0 0 5 第一个结果是表头
第二个结果是根据表头然后把Qty(数量)列在下面

解决方案 »

  1.   

    第1个查询
    create function dbo.f_GetAStr(@sizegroup varchar(5))
    returns varchar(2000)
    as
    begin
      declare @s varchar(2000)
      set @s=''
      select @s=@s+size+' ' from T_Temp where sizegroup=@sizegroup group by size
      return (@s)
    endSelect SizeGroup,Size=dbo.f_GetAStr(SizeGroup)
      From T_Temp 
     Group By SizeGroup
    /*
    SizeGroup Size
    A    46 48 50 52 54
    B    29 30 32 34 36
    E    29
    */
      

  2.   

    第2个查询
    declare @sql varchar(8000)
    select @sql='Select StyleNO,ColorName,SeaSon,Price,SizeGroup'
    select @sql=@sql+', sum(case when size='+size+' then Qty else 0 end)'
         from T_Temp a where SizeGroup='A'
         group by SizeGroup,Size;
    select @sql=@sql+',sum(Qty) from T_Temp where SizeGroup='+'''A'''+
      ' group by StyleNO,ColorName,SeaSon,Price,SizeGroup';
    select @sql=@sql+' union Select StyleNO,ColorName,SeaSon,Price,SizeGroup'
    select @sql=@sql+', sum(case when size='+size+' then Qty else 0 end)'
         from T_Temp a where SizeGroup='B'
         group by SizeGroup,Size;
    select @sql=@sql+',sum(Qty) from T_Temp where SizeGroup='+'''B'''+
      ' group by StyleNO,ColorName,SeaSon,Price,SizeGroup';
    select @sql=@sql+' union Select StyleNO,ColorName,SeaSon,Price,SizeGroup'
    select @sql=@sql+', sum(case when size='+size+' then Qty else 0 end)'
         from T_Temp a where SizeGroup='E'
         group by SizeGroup,Size;
    select @sql=@sql+',0,0,0,0,sum(Qty) from T_Temp where SizeGroup='+'''E'''+
        ' group by StyleNO,ColorName,SeaSon,Price,SizeGroup';exec(@sql)