解决方案 »

  1.   


    select 物资名称,
        [第一公司]= max(case when 采购单位 = '第一公司' then 数量 else 0 end),
        [第二公司]= max(case when 采购单位 = '第二公司' then 数量 else 0 end),
        [第三公司]= max(case when 采购单位 = '第三公司' then 数量 else 0 end),
        sum(数量) as sl,sum(金额) je, max(采购日期) rq 
    from ta 
    group by 物资名称
      

  2.   

    采购单位数量少的话直接用静态SQL就可以了。
      

  3.   

    1楼说的很对select 物资名称,
      [第一公司]= max(case when 采购单位 = '第一公司' then 数量 else 0 end),
      [第二公司]= max(case when 采购单位 = '第二公司' then 数量 else 0 end),
      [第三公司]= max(case when 采购单位 = '第三公司' then 数量 else 0 end),
      sum(数量) as sl,sum(金额) je, max(采购日期) rq 
    from ta 
    group by 物资名称用这个语句可以实现,但原理不太清楚,能详细说一下“max”,“then 数量 else 0 end”的意思吗?
      

  4.   

    楼主先去巩固一下聚合语句吧,语句中只是加了条件判断而已,去掉条件判断,剩下的仅是普通聚合语句了。
    如:select a,max(b) b,max(c) c from tb group by a
      

  5.   


    那就用动态SQL ,如一楼、四楼的写法
      

  6.   

    htl258说的也很好,但1楼先到,所以就给了90分。