总和SQL Server 2005中计数的百分比 [英] Sum & Percentage of Count in sql server 2005

查看:122
本文介绍了总和SQL Server 2005中计数的百分比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下情况

原始格式

SrNo 月 问题
1一月8
2 二月14
3 三月26
4 四月21
5 可能14
6 六月19
7 七月2

我需要在sql中使用以下格式

Sr.No.Month问题SUM  %
1一月8  104   7.69
2 二月14  104  13.46
3 三月26  104  25.92
4 四月21  104  20.19
5 可能14  104  13.46
6 六月19  104  18.32
7 七月2  104  0.96

I have following scenario

original format

SrNo Month  Issues
1  January   8
2  February  14
3  March  26
4  April  21
5  May  14
6  June  19
7  July  2

I need following format in sql

Sr.No.Month  Issues  SUM  %
1  January  8  104  7.69
2  February  14  104  13.46
3  March  26  104  25.92
4  April  21  104  20.19
5  May  14  104 13.46
6  June  19  104  18.32
7  July  2  104  0.96

推荐答案

这是sa(i)mple查询以获取结果:

Here is the sa(i)mple query to get the result:

Select SrNo, Month, Issues,
Sum(Issues) as SUM, 
(Issues * 100 / Sum(Issues)) as Percentage
From myTable


您可以使用 ^ ]和 OVER子句 [ ^ ].
You can use the SUM function[^] and the OVER clause[^].
SELECT SrNo, Month, Issues, SUM(Issues) OVER() AS SUM, CAST(Issues * (100.0 / SUM(Issues) OVER() AS DECIMAL (4, 2)) AS PERC
FROM myTable


CAST函数 [


The CAST function[^] is used to display the PERC with two digits.


这篇关于总和SQL Server 2005中计数的百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆