select item,times,times/(select sum(times)from surveyItem )*1.0 from surveyItem
求一个数据库中某一选项在所有数值的百分比,为什么都是0
求一个数据库中某一选项在所有数值的百分比,为什么都是0
解决方案 »
- 求一行转列问题,在线等
- 问个MySql语句的问题 如何查询时查出的纪录后自动加1
- 想写一个存储过程
- 现在在数据列表中插入UTF-8编码数据,在数据库中的 varchar类型是否要修改?
- 怎样把把小数点转换成百分数的形式??
- SQL server统计数据库表,各表的记录数
- 为什么在存储过程中执行事务时,当 @@TRANCOUNT 的值在完成存储过程时与执行存储过程时不同,会出现信息性错误 (266)?
- Sybase数据库里面建立了一个存储过程,在SQL下无任何问题,为什么在Powerbuilder 8自带的数据库Sybase AnyWhere 下就不能通过,帮忙解决
- sql
- mssql2012中的sequence问题
- sql
- csv导入mysql报错ERROR 1064 (42000)
from surveyItem
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%
/*------------------------------*/
;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
*/
;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%