-2

样本数据 :

LINE_NO E_FIELD F_FIELD G_FIELD HSA_STATUS  FAMILY  CACHE_FAMILY    Count
23053B  00000   00000   00000   S           SUMMIT      WER           43
23053B  00000   00000   00000   T           SUMMIT      WER           144
23053B  00000   00000   00684   T           SUMMIT      WER           2
23053B  00353   00418   00684   T           SUMMIT      WER           1
23053B  00353   00418   00763   T           SUMMIT      WER           1
23053B  00353   00418   01512   T           SUMMIT      WER           1
23053B  00353   00418   06797   T           SUMMIT      WER           1
23053B  00353   00418   30228   T           SUMMIT      WER           1
23053B  00353   00418   31935   T           SUMMIT      WER           2
23053B  05601   01402   00758   T           SUMMIT      WER           1
23053B  05601   01402   09091   T           SUMMIT      WER           1
23053B  05601   01402   65053   T           SUMMIT      WER           1
-------------------------------------------------------------------
23054B 00000 00000 00000 S                 SUMMIT      WER           37   
23054B 00000 00000 00000 T                 SUMMIT      WER           96    
23054B 00353 00418 00758 T                 SUMMIT      WER           1    
23054B 00354 00414 01095 T                  SUMMIT     WER           1     

这是我的查询:“ SELECT LINE_NO,E_FIELD,F_FIELD,G_FIELD,HSA_STATUS,FAMILY,CACHE_FAMILY,Count = ((SUM(TOTAL))) FROM(SELECT LINE_NO,E_FIELD,F_FIELD,G_FIELD,HSA_STATUS,FAMILY,CACHE_FAMILY,Count( LINE_NO)作为 TX_HSA_SUMM 的总和,其中 MT_TIMESTAMP2 在('2013-03-07 10:10:00')和('2013-03-08 10:20:00')按 LINE_NO、E_FIELD、F_FIELD、G_FIELD、HSA_STATUS 分组, FAMILY,CACHE_FAMILY) 作为 GROUP BY LINE_NO,E_FIELD,F_FIELD,G_FIELD,HSA_STATUS,FAMILY,CACHE_FAMILY,total BY LINE_NO,E_FIELD,F_FIELD,G_FIELD,HSA_STATUS,FAMILY,CACHE_FAMILY,total"

这实际上是我的示例数据。我已经算了。如您所见,列标题。我的问题是我需要对计数和我需要获得百分比的每一行求和。例如,上述记录的总数为199,因此第一条记录计数为43,因此计算应为43/199 * 100。如何查看百分比?这 ??平均列来获得百分比。请帮助我,我急需这个。它应该只在破折号之后的特定行中完成,它是一个新行,所以在计算百分比时,它应该只计算那个特定的行,而不是计算并获得百分比。

有什么建议可以显示如何首先获得计数。然后从总和中计算,最后。计数应除以总和和 * 100 以获得百分比。谢谢

4

3 回答 3

0

尝试使用以下查询:

select (Count/(select SUM(Count) from tablename))*100 from tablename;

这里SUM()用于查找列中值的总和。

如链接中所述,SUM 只能用于数字列。空值被忽略。

于 2013-03-20T08:56:59.380 回答
0

最简单的方法:创建一个临时表(比如@temp),将上面的结果插入其中并执行以下操作:

Declare @temp table
(
    LINE_NO varchar(20), 
    E_FIELD varchar(20),
    F_FIELD varchar(20),
    G_FIELD varchar(20),
   HSA_STATUS varchar(20),
   FAMILY varchar(20),
   CACHE_FAMILY varchar(20),
   CountValue int
 )
 Insert into @temp
 Your above result shown in your question

Declare @CountSum int
Set @CountSum=(Select Sum(CountValue) From @temp)

Select t.*,Cast(Cast((t.CountValue*@CountSum) as Decimal(10,3))/100 as decimal(5,2))    Count_Percentage From @temp t

它会给:

LINE_NO E_FIELD F_FIELD G_FIELD HSA_STATUS  FAMILY  CACHE_FAMILY    Count     Count_Percentage
=============================================================================================== 
23053B  00000   00000   00000   S           SUMMIT  WER               43        85.57
23053B  00000   00000   00000   T           SUMMIT  WER              144        286.56
23053B  00000   00000   00684   T           SUMMIT  WER              2          3.98
23053B  00353   00418   00684   T           SUMMIT  WER              1          1.99
23053B  00353   00418   00763   T           SUMMIT  WER              1          1.99
23053B  00353   00418   01512   T           SUMMIT  WER              1          1.99
23053B  00353   00418   06797   T           SUMMIT  WER              1          1.99
23053B  00353   00418   30228   T           SUMMIT  WER              1          1.99
23053B  00353   00418   31935   T           SUMMIT  WER              2          3.98
23053B  05601   01402   00758   T           SUMMIT  WER              1          1.99
23053B  05601   01402   09091   T           SUMMIT  WER              1          1.99
23053B  05601   01402   65053   T           SUMMIT  WER              1          1.99
于 2013-03-20T09:41:10.093 回答
0
;WITH T AS 
(
    SELECT 43 AS [Count]
    UNION ALL
    SELECT 144
    UNION ALL
    SELECT 2
    UNION ALL
    SELECT 1
    UNION ALL
    SELECT 1
    UNION ALL
    SELECT 1
    UNION ALL
    SELECT 1
    UNION ALL
    SELECT 1
    UNION ALL
    SELECT 2
    UNION ALL
    SELECT 1
    UNION ALL
    SELECT 1
    UNION ALL
    SELECT 1
)

SELECT 100.0 * T.[Count] / SUM(T.[Count]) OVER()
FROM T
于 2013-03-20T09:45:08.373 回答