基础数据: 
—————————————————————————————— 
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
——————————————————————————————

解决方案 »

  1.   

    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
    */
      

  2.   

    select
    ftpid,
    billid = max(case fieldid when 'billid' then content else 0 end),
    phone = max(case fieldid when 'phone' then content else 0 end),
    gid
    group by ftpid, gid
      

  3.   


    declare @table table (fptid varchar(5),fieldid varchar(6),content int,gid int,flag int)
    insert into @table
    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 * from (
    select a.fptid,a.[content] as billid ,b.[content] as phone 
    ,a.gid from @table a left join @table b
    on a.fptid=b.fptid and a.gid=b.gid
    and a.content<b.content
    ) aa where phone is not null
    /*
    fptid billid      phone       gid
    ----- ----------- ----------- -----------
    ezcat 123         445         1
    ezcat 346         567         2
    */
      

  4.   

    假設有中文呢?基础数据:  
    ——————————————————————————————  
    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
    ——————————————————————————————
      

  5.   


    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
    */
      

  6.   

    Dlut_LIuQ Master , 
    Thanks yours answer, 
    That's great!! 
    利害!!
      

  7.   

    <code=SQL>
    select fptid,billid,phone,gid from t2 pivot(max(content) for fieldid in(billid,phone)) t
    </code>
      

  8.   


    select fptid,billid,phone,gid from t2 pivot(max(content) for fieldid in(billid,phone)) t
    上面代码回复格式写错了
      

  9.   

    ls的都给出了正确的解答,lz还不结贴?