用sql语句将表1变成表2  太头痛了,求大侠解救~~
表1:
名称 价格 类型
a 10 p
a 10 p
a 10 p
a 10 p
a 11 l
b 9 l
b 9 l
b 8 w
c 8 l
c 7 p
c 7 p
c 10 w
d 10 w
d 10 w
d 11 l
d 15 p表2:
名称 类型l-数量 类型l-价格 类型P-数量 类型P-价格 类型w-数量 类型w-价格
a 1 11 4 10 0 0
b 2 9 0 0 1 8
c 1 8 2 7 0 0
d 1 11 1 15 3 10

解决方案 »

  1.   

    行转列问题
    http://topic.csdn.net/u/20080614/17/22e73f33-f071-46dc-b9bf-321204b1656f.html
    研究一下自己写写吧
      

  2.   

    create table test (
    名称 varchar(20) ,价格 float, 类型 varchar(20))
    insert into test values('a', 10, 'p')
    insert into test values('a', 10, 'p')
    insert into test values('a', 10, 'p')
    insert into test values('a', 10, 'p')
    insert into test values('a', 11, 'l')
    insert into test values('b', 9, 'l')
    insert into test values('b', 9, 'l')
    insert into test values('b', 8, 'w')
    insert into test values('c', 8, 'l')
    insert into test values('c', 7, 'p')
    insert into test values('c', 7, 'p')
    insert into test values('c', 10, 'w')
    insert into test values('d', 10, 'w')
    insert into test values('d', 10, 'w')
    insert into test values('d', 11, 'l')
    insert into test values('d', 15, 'p')
    go
     select 名称
     ,isnull(sum((case 类型 when 'l' then 数量 end )),0) as 类型l_数量
     ,isnull(sum((case 类型 when 'l' then 价格 end )),0) as 类型l_价格
     ,isnull(sum((case 类型 when 'p' then 数量 end )),0) as 类型p_数量
     ,isnull(sum((case 类型 when 'p' then 价格 end )),0) as 类型p_价格
     ,isnull(sum((case 类型 when 'w' then 数量 end )),0) as 类型w_数量
     ,isnull(sum((case 类型 when 'w' then 价格 end )),0) as 类型w_价格
     from 
     ( select 名称,价格,类型,COUNT(类型) as 数量 from test group  by 名称,价格,类型) a
      group by 名称
     你的结果有误 或者你的测试数据有问题:
    数据如下:
    a 1 11 4 10 0 0
    b 2 9 0 0 1 8
    c 1 8 2 7 1 10
    d 1 11 1 15 2 10