select item,times,times/(select sum(times)from surveyItem )*1.0  from surveyItem 
求一个数据库中某一选项在所有数值的百分比,为什么都是0

解决方案 »

  1.   

    try this,select item,times,rtrim(times*1.0/(select sum(times)from surveyItem)*1.0*100)+'%'
     from surveyItem 
      

  2.   


    declare @tb table(ID INT IDENTITY PRIMARY KEY ,
                      surveyID INT NOT NULL,
                      Item VARCHAR(20) NOT NULL,
                      times INT NOT NULL DEFAULT 0
                      )
    insert into @tb
    SELECT  1, '很有必要',10  union all
    select  1, '完全没有必要',20 union all
    select  1,'无所谓',15--SELECT  *  FROM @tb
     select item,times,
     rate=CONVERT(VARCHAR,ROUND(CAST(times AS INT)/CAST((select sum(times)from @tb) AS FLOAT(4))*100,2))+'%'
     from @tb/*------------------------------*/
    item                 times         rate
    很有必要     10    22.22%
    完全没有必要    20    44.44%
    无所谓            15            33.33%
    /*------------------------------*/
      

  3.   


     
    ;WITH tb(ID,Item,times)
    AS (
        SELECT  1, N'很有必要',10  union all
        select  2, N'完全没有必要',20 union all
        select  3,N'无所谓',15
    )  select item,times,
     rate=round(times*1.0 /SUM(times)OVER(PARTITION BY GETDATE()),2)
     from tb
    /*
    item times rate
    很有必要 10 0.220000000000
    完全没有必要 20 0.440000000000
    无所谓 15 0.330000000000
    */
      

  4.   


    ;WITH tb(ID,Item,times)
    AS (
        SELECT  1, N'很有必要',10  union all
        select  2, N'完全没有必要',20 union all
        select  3,N'无所谓',15
    )  select item,times,
     rate=convert(varchar,convert(float,round(times*1.0 /SUM(times)OVER(PARTITION BY GETDATE()),4))*100)+'%'
     from tbitem times rate
    很有必要 10 22.22%
    完全没有必要 20 44.44%
    无所谓 15 33.33%