我试图在列上使用公式,我想要的是我在列中有一个值我想将它乘以(100 / SumofColumnValues)
我成功到现在为止得到的总数
select
Count(*) as ResponseCount,
PropertyValue As Answer
from
table
where
Questionid = 42 and formid = 1
group by
propertyvalue
这就是这样的
ResponseCount Answers
34 One
100 Two
然后使用CTE:
With Temp As (
select
PropertyValue As Answers,
Count(*) As ResponseCount
from
questionerdetail
where
Questionid = 42 and formid = 1
group by
PropertyValue
)
select Sum(ResponseCount) As Total
from Temp
我得到了
Total
134
我需要的是
ReponseCount Answer ResponsePercentage
34 One 25.37 TheFormula will be 34*(100/134)- (134 is the total sum of responsecount)
100 Two 74.62 TheFormula Will be 100*(100/134)
答案 0 :(得分:2)
或者如果您想使用分析函数而不是子选择:
select
Count(*) as ResponseCount,
PropertyValue As Answer ,
convert ( dec(28,2) ,Count(*))*100/(sum(count(*)) over ()) as ResponsePercentage
from
table
where
Questionid = 42 and formid = 1
group by
propertyvalue
(转换函数是SQL Server语法)
答案 1 :(得分:1)
select
Count(*) as ResponseCount,
PropertyValue As Answer ,
Count(*)*100/(select count(*) from table ) as ResponsePercentage
from
table
where
Questionid = 42 and formid = 1
group by
propertyvalue