原数据:ID    类型    值
A     11      2000
A     12      2400
A     13      2900
A     14      3000
B     11      9000
B     12      2000
B     13      2400
B     14      2900
C     11      3000
C     12      9000写一sql语句,使之成为如下数据:
ID    值1    值2    值3    值4  
A     2000   2400   2900   3000
B     9000   2000   2400   2900
C     3000   9000

解决方案 »

  1.   

    select
      id,
      max(case 类型 when 11 then 值 else 0 end) as 值1,
      max(case 类型 when 12 then 值 else 0 end) as 值2,
      max(case 类型 when 13 then 值 else 0 end) as 值3,
      max(case 类型 when 14 then 值 else 0 end) as 值4
    from
      tb
    group by
      id
      

  2.   

    select id , 
           max(case px when 1 then cast(值 as varchar) else '') 值1,
           max(case px when 2 then cast(值 as varchar) else '') 值2,
           max(case px when 3 then cast(值 as varchar) else '') 值3,
           max(case px when 4 then cast(值 as varchar) else '') 值4
    from
    (
      select t.* , px = (select count(1) from tb where id = t.id and 值 < t.值) + 1 from tb t
    ) m
    group by id
      

  3.   

    create table tb(ID varchar(10),类型 int,值 int)
    insert into tb select 'A',11,2000
    insert into tb select 'A',12,2400
    insert into tb select 'A',13,2900
    insert into tb select 'A',14,3000
    insert into tb select 'B',11,9000
    insert into tb select 'B',12,2000
    insert into tb select 'B',13,2400
    insert into tb select 'B',14,2900
    insert into tb select 'C',11,3000
    insert into tb select 'C',12,9000
    go
    select id,sum(case when 类型=11 then 值 else 0 end)值1,
    sum(case when 类型=12 then 值 else 0 end)值2,
    sum(case when 类型=13 then 值 else 0 end)值3,
    sum(case when 类型=14 then 值 else 0 end)值4
    from tb group by id
    /*
    id         值1          值2          值3          值4
    ---------- ----------- ----------- ----------- -----------
    A          2000        2400        2900        3000
    B          9000        2000        2400        2900
    C          3000        9000        0           0(3 行受影响)*/
    go
    drop table tb
      

  4.   

    create table tb(ID varchar(10),类型 int,值 int)
    insert into tb values('A', 11 ,2000)
    insert into tb values('A', 12 ,2400)
    insert into tb values('A', 13 ,2900)
    insert into tb values('A', 14 ,3000)
    insert into tb values('B', 11 ,9000)
    insert into tb values('B', 12 ,2000)
    insert into tb values('B', 13 ,2400)
    insert into tb values('B', 14 ,2900)
    insert into tb values('C', 11 ,3000)
    insert into tb values('C', 12 ,9000)
    go--如果你根据类型来算
    select id , 
           max(case 类型 when 11 then cast(值 as varchar) else '' end) 值1,
           max(case 类型 when 12 then cast(值 as varchar) else '' end) 值2,
           max(case 类型 when 13 then cast(值 as varchar) else '' end) 值3,
           max(case 类型 when 14 then cast(值 as varchar) else '' end) 值4
    from tb
    group by id
    /*
    id         值1                             值2                             值3                             值4                             
    ---------- ------------------------------ ------------------------------ ------------------------------ ------------------------------ 
    A          2000                           2400                           2900                           3000
    B          9000                           2000                           2400                           2900
    C          3000                           9000                                                          (所影响的行数为 3 行)
    */--如果你根据ID其值大小来算
    select id , 
           max(case px when 1 then cast(值 as varchar) else '' end) 值1,
           max(case px when 2 then cast(值 as varchar) else '' end) 值2,
           max(case px when 3 then cast(值 as varchar) else '' end) 值3,
           max(case px when 4 then cast(值 as varchar) else '' end) 值4
    from
    (
      select t.* , px = (select count(1) from tb where id = t.id and 值 < t.值) + 1 from tb t
    ) m
    group by id
    /*
    id         值1                             值2                             值3                             值4                             
    ---------- ------------------------------ ------------------------------ ------------------------------ ------------------------------ 
    A          2000                           2400                           2900                           3000
    B          9000                           2000                           2400                           2900
    C          3000                           9000                                                          (所影响的行数为 3 行)
    */drop table tb
      

  5.   

    select id , 
           max(case 类型 when 11 then cast(值 as varchar) else '' end) 值1,
           max(case 类型 when 12 then cast(值 as varchar) else '' end) 值2,
           max(case 类型 when 13 then cast(值 as varchar) else '' end) 值3,
           max(case 类型 when 14 then cast(值 as varchar) else '' end) 值4
    from #tb
    group by id
      

  6.   

    请问一下,这里的max起到什么作用,为啥要用最大值函数呢?
      

  7.   

    将MAX去掉,看一下数据你就应该清楚了