select giftname  count(giftid)[count], sum(amount) sum2 from gift  group by giftname

解决方案 »

  1.   

    select giftname , count(giftid)[count], sum(amount) sum2 from gift  group by giftname
      

  2.   

    select 
      giftname,
      count(giftid) as [count],
      sum(amount) as [sum2]
    from 表
    group by giftname
      

  3.   


    --TRYselect giftname,count(giftname)as [count],sum(amount) as sum2 from table0 group by giftname
      

  4.   

    select giftname , max(giftid) , sum(amount) from tb group by giftname
      

  5.   

    select giftname , max(giftid) , sum(amount) from tb group by giftname
    select giftname , count(giftid) , sum(amount) from tb group by giftname
      

  6.   

    if object_id('tempdb..#')is not null drop table #
    go
    create table #(giftname varchar(10),  giftid int, amount int, type varchar(10)) 
    insert # select 'aaa' ,         1,    12 ,   'lll' 
    insert # select 'aaa' ,         2 ,   23 ,   'lll' 
    insert # select 'bbb',          3 ,   14,   ' iii' 
    select giftname , count(giftid)[count], sum(amount) sum2 from #  group by giftname
    /*giftname   count       sum2        
    ---------- ----------- ----------- 
    aaa        2           35
    bbb        1           14
    */
      

  7.   

    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb]([giftname] varchar(3),[giftid] int,[amount] int,[type] varchar(3))
    insert [tb]
    select 'aaa',1,12,'lll' union all
    select 'aaa',2,23,'lll' union all
    select 'bbb',3,14,'iii'select 
      giftname,
      count(giftid) as [count],
      sum(amount) as [sum2]
    from tb
    group by giftname
    --测试结果:
    giftname count       sum2        
    -------- ----------- ----------- 
    aaa      2           35
    bbb      1           14(所影响的行数为 2 行)
      

  8.   

    create table #T
    (
    giftname nvarchar(20),
     giftid int,
      amount int,
      [type] nvarchar(20)

    insert into #T
    select 'aaa' ,         1  ,  12 ,   'lll' union all 
    select 'aaa',          2  ,  23 ,   'lll' union all
    select 'bbb',          3  ,  14 ,   'iii'
    select giftname,COUNT(giftname),SUM(amount) from #T group by giftname