如何准确计算有限总数的百分比 [英] How to accurately figure percentage of a finite total

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

问题描述

我正在创建一个报告,告诉最终用户在给定的月份中gridview占多少百分比(记录的总数是有限数量)。我有一个gridview,包含我导入的记录,用户必须进入每条记录并更新几个字段。我试图创建一份报告,告诉我在特定月份中完成的记录总数的百分比。我需要的只是百分比。总的来说(在这个例子中)是2000.


我不确定在这里是否需要实际的gridview信息/代码,但如果它确实需要,请告诉我,我将添加它。


问题是我已经能够计算总的百分比,但是当它显示时,表中的每一行重复的百分比总数。我正在摸索如何让这个结果只出现一次。


现在这里是我的SQL代码(我使用nvarchar,因为我们从很多非Windows系统导入并得到所有类型的额外字符和添加空间到我们的信息):

I’m in the process of creating a report that will tell end users what percentage of a gridview (the total number of records is a finite number) has been completed in a given month. I have a gridview with records that I’ve imported and users have to go into each record and update a couple of fields. I’m attempting to create a report tell me what percentage of the grand total of records was completed in a given month. All I need is the percentage. The grand total is (for this example) is 2000.

I’m not sure if the actual gridview information/code is needed here but if it does, let me know and I’ll add it.

The problem is that I have been able to calculate the percentage total but when its displayed the percentage total is repeated for every single line in the table. I’m scratching my head on how to make this result appear only once.

Right now here’s what I have for my SQL code (I use nvarchar because we import from many non windows systems and get all sorts of extra characters and added spaces to our information):

Declare @DateCount nvarchar(max);
Declare @DivNumber decimal(5,1);

SET @DivNumber = (.01 * 2541);
SET  @DateCount = (SELECT (Count(date_record_entered) FROM dbo.tablename WHERE date_record_entered IS NOT NULL and date_record_entered >= 20131201 AND date_record_entered <= 20131231); 

SELECT CAST(ROUND(@DivNumber / @DateCount, 1) AS decimal(5,1) FROM dbo.tablename

对于这个例子来说,12月份的date_record_entered中的记录总数是500.


我已经单独尝试了一小段代码,但没有成功。这是最近的事情我试过。


我知道我在这里错过了一些简单的东西,但我不确定是什么。

Let’s say for this example the total number of records in the date_record_entered for the month of December is 500.

I’ve tried the smaller pieces of code separately with no success. This is the most recent thing I’ve tried.

I know I'm missing something simple here but I'm not sure what.

:::

我所查找的结果是我的查询的预期结果是在给定月份中修改记录的百分比。如果500条记录完成了25%,我只希望有25个(和适用的小数点)表示一次,而不是25个表示e

What I'm looking for as the expected result of my query is to have a percentage represented of records modified in a given month. If 500 records were done that would be 25%. I just want to have the 25 (and trainling decimal(s) when it applies) showing once and not 25 showing for every row in this table.

推荐答案

以下查询应该提供您正在寻找的内容:

The following query should provide what you are looking for:

Declare @DivNumber decimal(5,1);

SET @DivNumber = (.01 * 2541);

SELECT 
    CAST(ROUND(@DivNumber / Count(date_record_entered), 1) AS decimal(5,1))
  FROM dbo.tablename 
 WHERE date_record_entered IS NOT NULL 
   and date_record_entered >= 20131201 
   AND date_record_entered <= 20131231

这篇关于如何准确计算有限总数的百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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