select b,sum(a),(sum(a)*1.0)/((select sum(a) from temp)*1.0)*100 from temp group by b

解决方案 »

  1.   

    select 商品,sum(數量)as 總數  form xxx into #tmp1 group by 商品
    select 商品,sum(數量)as 出售總數 as  form xxx  #tmp2 group by 商品 where 出售日期 is not nullselect #tmp1.商品,-#tmp2.出售總數,-#tmp2.出售總數 ,,#tmp2.出售總數/#tmp1.總數就是这样
      

  2.   

    select 商品,a=sum(數量),c=(sum(數量)*1.0)/((select sum(數量) from 商品表 where 出售日期 is not null )*1.0)*100 into #temp1 from 商品表 where 出售日期 is not null group by 商品select 商品,a=sum(數量),c=(sum(數量)*1.0)/((select sum(數量) from 商品表 where 出售日期 is null )*1.0)*100 into #temp2 from 商品表 where 出售日期 is null group by 商品select #temp1.商品,出售總數=#temp1.a,占總出售數的百份比=#temp1.c,未出售總數=#temp2.a,占未出售數的百份比=#temp2.c from #temp1,#temp2 where #temp1.商品=#temp2.商品
    drop table #temp1
    drop table #temp2