规则,如果浓度一样,浓度不变,数量求和,否则不改变原来的逻辑。
Group Sample 浓度 数量
GroupA KK1 1 16
GroupA KK2 2 17
GroupA KK3 3 18
GroupA KK4 4 19
GroupB KK1 5 20
GroupB KK2 5 21
GroupB KK3 5 22
GroupB KK4 5 23
GroupC KK1 16 24
GroupC KK2 15 25
GroupC KK3 19 26
GroupC KK4 30 27
’=》
Group Sample 浓度 数量
GroupA KK1 1 16
GroupA KK2 2 17
GroupA KK3 3 18
GroupA KK4 4 19
GroupB Sum 5 86(20+21+22+23)
 
GroupC KK1 16 24
GroupC KK2 15 25
GroupC KK3 19 26
GroupC KK4 30 27

解决方案 »

  1.   

    select [Group],min(Sample) Sample,浓度,sum(数量) 数量 from tb
     group by [Group],浓度
      

  2.   


    declare @table table ([Group] varchar(6),Sample varchar(3),浓度 int,数量 int)
    insert into @table
    select 'GroupA','KK1',1,16 union all
    select 'GroupA','KK2',2,17 union all
    select 'GroupA','KK3',3,18 union all
    select 'GroupA','KK4',4,19 union all
    select 'GroupB','KK1',5,20 union all
    select 'GroupB','KK2',5,21 union all
    select 'GroupB','KK3',5,22 union all
    select 'GroupB','KK4',5,23 union all
    select 'GroupC','KK1',16,24 union all
    select 'GroupC','KK2',15,25 union all
    select 'GroupC','KK3',19,26 union all
    select 'GroupC','KK4',30,27select [Group],max(Sample) as Sample,浓度,
    sum(数量) 数量 from @table group by [Group],浓度
    /*
    Group  Sample 浓度          数量
    ------ ------ ----------- -----------
    GroupA KK1    1           16
    GroupA KK2    2           17
    GroupA KK3    3           18
    GroupA KK4    4           19
    GroupB KK4    5           86
    GroupC KK2    15          25
    GroupC KK1    16          24
    GroupC KK3    19          26
    GroupC KK4    30          27
    */
      

  3.   

    declare @table table ([Group] varchar(6),Sample varchar(3),浓度 int,数量 int)
    insert into @table
    select 'GroupA','KK1',1,16 union all
    select 'GroupA','KK2',2,17 union all
    select 'GroupA','KK3',3,18 union all
    select 'GroupA','KK4',4,19 union all
    select 'GroupB','KK1',5,20 union all
    select 'GroupB','KK2',5,21 union all
    select 'GroupB','KK3',5,22 union all
    select 'GroupB','KK4',5,23 union all
    select 'GroupC','KK1',16,24 union all
    select 'GroupC','KK2',15,25 union all
    select 'GroupC','KK3',19,26 union all
    select 'GroupC','KK4',30,27select [Group],max(Sample) as Sample,浓度,
    sum(数量) 数量 from @table group by [Group],浓度
    /*
    Group  Sample 浓度          数量
    ------ ------ ----------- -----------
    GroupA KK1    1           16
    GroupA KK2    1           17
    GroupA KK3    3           18
    GroupA KK4    4           19
    GroupB KK4    5           86
    GroupC KK2    15          25
    GroupC KK1    16          24
    GroupC KK3    19          26
    GroupC KK4    30          27

    希望全一样才统计,另外 
    GroupB KK4    5           86
    变成SUM总和
      

  4.   

    select GroupName,max([sample]) as [sample],浓度,max(数量) AS 数量 from table1 group by GroupName,浓度 having count(*)=1
    union ALL
    SELECT  GroupName,'Sum' as [sample],浓度,sum(数量) AS 数量 from table1 group by GroupName,浓度 having count(*)>1以上是按照GroupName 和浓度为分组的,这个你在提问时也没说清楚,当浓度一样,单GroupName不一样的,是否要相加。