SQL查询问题,按要求GROUP   BY以后,列出统计表。 

解决方案 »

  1.   

    select id,max(value),min(value),avg(value),count(value) from tb group by id
      

  2.   

    呵呵,楼主应该说清楚"要求"是什么?按哪些字段GROUP BY,统计出来什么数据?
      

  3.   

    基础数据: 
    —————————————————————————————— 
    fptid   fieldid content gid     flag
    ezcat billid 123 1 1
    ezcat phone 445 1 1
    ezcat phone 346 2 1
    ezcat billid 567 2 1
    ——————————————————————————————  
    要求:可否转成下面様子? 
    —————————————————————————————— 
    ftpid     billid    phone      gid
    ezcat     123      445        1
    ezcat     346      567        2
    ——————————————————————————————
      

  4.   

    create table tb
    (
    fptid varchar(10),
    fieldid  varchar(10),
    content int,
    gid int,
    flag int
    )insert into tb
    select 'ezcat','billid',123,1,1 union all
    select 'ezcat','phone',445,1,1 union all
    select 'ezcat','phone',346,2,1 union all
    select 'ezcat','billid',567,2,1select fptid
    ,sum(case when fieldid='billid' then content else 0 end) as billid
    ,sum(case when fieldid='phone' then content else 0 end) as phone
    ,gid
    from tb group by fptid,gid
    /*
    ------------------------
    fptid billid phone gid
    ezcat 123 445 1
    ezcat 567 346 2
    */
      

  5.   

    假設有中文呢?基础数据:  
    ——————————————————————————————  
    fptid fieldid content gid flag
    ezcat billid 123 1 1
    ezcat phone 551 1 1
    ezcat address 上海 1 1
    ezcat billid 346 1 1
    ezcat phone 789 2 1
    ezcat address 北京 1
    ——————————————————————————————   
    要求:可否转成下面様子?  
    ——————————————————————————————  
    ftpid billid phone address gid
    ezcat 123    551   上海     1
    ezcat 346    789   北京     2
    ——————————————————————————————
      

  6.   


    create table tb
    (
    fptid varchar(10),
    fieldid  varchar(10),
    content nvarchar(10),
    gid int,
    flag int
    )insert into tb
    select 'ezcat','billid','123',1,1 union all
    select 'ezcat','phone','445',1,1 union all
    select 'ezcat','phone','346',2,1 union all
    select 'ezcat','billid','567',2,1 union all
    select 'ezcat','address','上海',1,1 union all
    select 'ezcat','address','北京',2,1
    select fptid
    ,max(case when fieldid='billid' then content end) as billid
    ,max(case when fieldid='phone' then content end) as phone
    ,max(case when fieldid='address' then content end) as address
    ,gid
    from tb group by fptid,gid
    /*
    ----------------------
    fptid billid phone address gid
    ezcat 123 445 上海 1
    ezcat 567 346 北京 2
    */