sql - 最大投票百分比
问题描述
假设一个小镇正在举行选举,每个公民都可以投票多次,但他们投票的次数越多,他们的选票就会分裂。
我的输入如下
表:town_elections
Voter Vote
Jane Jane
Howard Jane
John Howard
John Jane
Jane John
Sara Howard
期待:
Jane Jane 0.33
Howard Jane 0.33
John Howard 0.5
John Jane 0.33
Jane John 1.0
Sara Howard 0.5
解决方案
试试下面的查询。cast to decimal
在这里使用您想要的结果
with MyCTE
as
(
select
voter,
Vote,
count(*) over(PARTITION BY Vote order
by Vote) as votecount
from town_elections
)
select Voter,Vote, cast((cast(100 as decimal(18,2))/cast(votecount as
decimal(18,2)))/100.00 as decimal(18,2)) as percentage from MyCTE
推荐阅读
- r - R自动将字符变量转换为整数变量
- blazor - InputText 需要“ValueExpression”参数的值
- ios - Swift String 和 base64Encode 混淆
- android - 如何知道应用程序在 Google Play 商店中的排名?
- ios - 如何设置 ViewController 的语言而不是设备的语言?
- google-cloud-firestore - get() 不起作用,错误不遵循文档
- swift - 奖励视频广告在我的标签上显示奖励内容
- android - 如何解决“问题:违反家庭政策要求”
- java - Java HttpsServer:第一次成功请求后连接超时
- isabelle - 在 Isabelle 中将 int 转换为 nat