下面是表的内容
字段  A1   B1    A2    B2    A3    B3  
      红   15    蓝    10    紫    20
      青   13    红    14    蓝    15
      白   30    紫    22    红    11
请问如何在A(A1,A2,A3)中归类每种颜色的重量B(B1,B2,B3)
例如:红色的重量=15+14+11
是不是很难?有没高手能想出来啊?

解决方案 »

  1.   


    declare @t table (A1 varchar(2) ,B1 float ,A2 varchar(2) ,B2 float ,A3 varchar(2) ,B3 float)insert into @t
    select '红',15,'蓝',10,'紫',20
    union all
    select '青',13,'红',14,'蓝',15
    union all
    select '白',30,'紫',22,'红',11SELECT A ,SUM(B) B FROM (
    select A1 A,SUM(b1) B
    FROM @t GROUP BY a1
    UNION ALL
    select A2,SUM(b2)
    FROM @t GROUP BY a2
    UNION ALL
    select A3,SUM(b3)
    FROM @t GROUP BY a3)T GROUP BY A
    (3 行受影响)
    A    B
    ---- ----------------------
    白    30
    红    40
    蓝    25
    青    13
    紫    42(5 行受影响)
      

  2.   

    declare @t table (A1 varchar(2) ,B1 float ,A2 varchar(2) ,B2 float ,A3 varchar(2) ,B3 float)
    insert into @t
    select '红',15,'蓝',10,'紫',20
    union all
    select '青',13,'红',14,'蓝',15
    union all
    select '白',30,'紫',22,'红',11select A1 as 颜色,sum(B1) as 重量
    from(select A1,B1 from @t
    union all select A2,B2 from @t
    union all select A3,B3 from @t) t
    group by A1----------------------------
    颜色 重量
    白 30
    红 40
    蓝 25
    青 13
    紫 42
      

  3.   

    declare @t table (A1 nvarchar(2) ,B1 float ,A2 nvarchar(2) ,B2 float ,A3 nvarchar(2) ,B3 float)insert into @t
    select N'红',15,N'蓝',10,N'紫',20
    union all
    select N'青',13,N'红',14,N'蓝',15
    union all
    select N'白',30,N'紫',22,N'红',11select A1,sum(B1) from(
    select A1,B1 from @t
    union all
    select A2,B2 from @t
    union all
    select A3,B3 from @t)
    x group by A1