试下这个:
select a.公司,a.名称,值=case b.值 when 0 then a.值 else isnull(a.值,0)/b.值 from (select * from 表 where 名称 in ('产值','销售')) a,(select * from 表 where 名称 in ('件数')) b where a.公司= b.公司

解决方案 »

  1.   

    有错,这样:
    select a.公司,a.名称,值=case b.值 when 0 then a.值 else isnull(a.值,0)/b.值 end from (select * from 表 where 名称 in ('产值','销售')) a,(select * from 表 where 名称 in ('件数')) b where a.公司= b.公司
      

  2.   

    select a.公司,a.名称,a.值/b.值
    from tableName a, tableName b
    where a.公司=b.公司 and b.名称='件数' and a.名称<>'件数'
      

  3.   

    不知道是不是这样:
    create table mm(公司 varchar(10),名称 varchar(20),值 int)
    insert mm  select 'A','产值',1200
    union all select 'A','销售',800
    union all select 'A','件数',10
    select 公司,名称, 值/(select 值 from mm where 名称='件数') as 值
    from mm where  名称='产值'
    union
    select 公司,名称,值/(select 值 from mm where 名称='件数') as 值 
    from mm where  名称='销售'
    不知道有没有更好的方法
      

  4.   

    to:Ncaidexiaoniao() create table mm(公司 varchar(10),名称 varchar(20),值 int)
    insert mm  select 'A','产值',1200
    union all select 'A','销售',800
    union all select 'A','件数',10
    select 公司,名称, 值/(select 值 from mm where 名称='件数') as 值
    from mm where  名称='产值'
    union
    select 公司,名称,值/(select 值 from mm where 名称='件数') as 值 
    from mm where  名称='销售'这个当公司不只一个公司时呢?
      

  5.   

    select *
    ,值/isnull((select sum(值) from 表 where 名称 = '件数' and 公司 = a.公司),1)
    from 表 a
    where 名称 in ('产值','销售')
    order by 公司,名称
      

  6.   

    select 公司,  名称 , 值/(select 值 from mm where 名称='件数') from mm
      

  7.   


    select a.公司,a.名称,(case b.值 when 0 then 0 else a.值/b.值 end)as 值 from
    (
    select 公司,名称,值 from #mm where 名称<>'件数'
    )a 
    left join
    (
    select 公司,名称,isnull(值 ,0) as 值 from #mm where 名称='件数'
    )b
    on a.公司=b.公司