create procedure spABC
asdeclare @countall float
select @countall = count(*) from table
select left(mid, 4) as 地区, count(*) as 数量, cast(count(*) * 100 / @countall as int) as 百分比 from table group by left(mid,4)

解决方案 »

  1.   

    select left(mid,4),count(*) 次数, cast(count(*)*1.00/(select count(*) from table)*100 as varchar) +'%' as 百分比 from table group by left(mid,4)
      

  2.   

    Select left(mid,4) as 地区,count(*) as 数量,
    count(*)*100.0/(Select count(*) from table)  as 百分比
    from table group by left(mid,4)
      

  3.   


    select left(mid,4),count(*) 次数, cast(count(*)*1.00/(select count(*) from table)*100 as varchar) +'%' as 百分比 from table group by left(mid,4)
      

  4.   

    select left(mid,4) mid,count(*) 数目,cast(count(*)/(select sum(1)+0.0 from [table])*100 as int) 百分比 from [table] group by left(mid,4)
      

  5.   

    create proc p_test
    as
    declare @总数 decimal(20,2)
    select @总数=count(*) from 表
    select *,百分比=cast(cast(数量/@总数 * 100 as decimal(20,2) as varchar)+'%'
    from(
    select 地区=left(mid,4),数量=(count(*)
    from 表 group by 地区) a
      

  6.   

    select left(mid,4) mid,count(*) 数目,cast(count(*)/(select sum(1)+0.0 from [table])*100 as int) 百分比 from [table] group by left(mid,4)
      

  7.   

    select left(mid,4) mid,count(*) 数目,cast(count(*)/(select sum(1)+0.0 from [table])*100 as int) 百分比 from [table] group by left(mid,4)
      

  8.   

    如果用在postgre数据库中呢
    上面的 语法可以么?
      

  9.   

    select left(mid,4) mid,count(*) 数目,cast(count(*)/(select sum(1)+0.0 from [table])*100 as int) 百分比 from [table] group by left(mid,4)
      

  10.   

    create function query(int) returns int 
    as'
       declare 
          countall ALIAS;
       begin
          select countall := count(*) from user_info
          select substring(mid,1,4) as diqu, count(*) as num, cast(count(*) * 100 / @countall as int) as cent from user_info group by substring(mid,1,4)
       end
    end
    ' language ';总说没有结束符号
    错在哪里
      

  11.   

    BEGIN TRAN TEST
    Create table mid(mid VarChar(6))
    INSERT INTO mid(mid) values ('000111')
    INSERT INTO mid(mid) values ('000122')
    INSERT INTO mid(mid) values ('000133')
    INSERT INTO mid(mid) values ('000211')
    DECLARE @Num INT
    SET @NUM=(SELECT COUNT(*) FROM mid)
    SELECT SUBSTRING(mid,1,4) AS 地区,
           @Num AS 总数,
           百分比=cast(cast(COUNT(*)*100/@Num as decimal(20,2)) as varchar)+'%'
      FROM mid
      GROUP BY SUBSTRING(mid,1,4)
    DROP TABLE mid
    COMMIT TRAN TEST------
    000111
    000122
    000133
    000211
    ------
    0001 4 75.00%
    0002 4 25.00%
      

  12.   

    create proc p_test
    as
    declare @总数 decimal(20,2)
    select @总数=count(*) from 表
    select *,百分比=cast(cast(数量/@总数 * 100 as decimal(20,2) as varchar)+'%'
    from(
    select 地区=left(mid,4),数量=(count(*)
    from 表 group by 地区) a
    select left(mid,4),count(*) 次数, cast(count(*)*1.00/(select count(*) from table)*100 as varchar) +'%' as 百分比 from table group by left(mid,4)