百分比行占总数,其中每行由 group by 子句确定

Posted

技术标签:

【中文标题】百分比行占总数,其中每行由 group by 子句确定【英文标题】:Percentage row makes up of total where each row determine by group by clause 【发布时间】:2010-09-15 14:46:30 【问题描述】:

我确定这是一个愚蠢的 n00b 问题,但我有以下结果表(它是通过我只了解一半的冗长查询创建的),我希望能够添加两个计算出相对于总数的行百分比的列:

示例结果表:

CREATE TABLE #temp
(category varchar(30)
,count_people INT
,numpayments05 INT
,avegifts05 DECIMAL(4,2)
,value05 DECIMAL(18,2)
,donorvalue05 DECIMAL(18,2)
,giftvalue05 DECIMAL(18,2)
)
INSERT INTO #temp VALUES ('Single Gifts',124945,182458.00,1.46,28787034.10,230.40,157.77)
INSERT INTO #temp VALUES ('New Donor',67598,78799.00,1.17,5915450.93,87.51,75.07)
INSERT INTO #temp VALUES ('Reactivated',19853,27394.00,1.38,4348419.38,219.03,158.74)
INSERT INTO #temp VALUES ('2yrs consecutive',20604,31633.00,1.54,3556766.75,172.63,112.44)
INSERT INTO #temp VALUES ('3/4yrs consecutive',7536,13251.00,1.76,1827917.34,242.56,137.95)
INSERT INTO #temp VALUES ('5+yrs consecutive',9354,31381.00,3.35,13138479.70,1404.58,418.68)

第一行,“单件礼物”是主查询上汇总子句的结果(不确定这是否会影响计算百分比(这意味着单件礼物类别本质上是每列的总值),并且每个类别选项派生自应用于查询的 group by 子句。

我想要的是能够在两个新列中添加“价值百分比”和“count_people 的百分比”,其中价值百分比 = 行 value05 / 总 value05(这是“单身礼物的 value05” ' 类别)。 count_people 的百分比将是 count_people 的行值,再次除以总数(这是包含在“单一礼物类别行”中的内容

我使用的是 SQL Server 2005,以防万一。

谢谢!

【问题讨论】:

【参考方案1】:

此查询将按行为您提供百分比:

SELECT category, count_people, numpayments05, avegifts05, value05, donorvalue05, giftvalue05,
    value05 / SUM(CASE category WHEN 'Single Gifts' THEN value05 ELSE 0.0 END)
        OVER (PARTITION BY 1) AS [percentage of value],
    count_people / SUM(CASE category WHEN 'Single Gifts' THEN count_people ELSE 0.0 END)
        OVER (PARTITION BY 1) AS [percentage of count_people]
FROM #temp ;

假设您使用以下内容在 #temp 中创建两个新列:

CREATE TABLE #temp
(category varchar(30)
,count_people INT
,numpayments05 INT
,avegifts05 DECIMAL(4,2)
,value05 DECIMAL(18,2)
,donorvalue05 DECIMAL(18,2)
,giftvalue05 DECIMAL(18,2)
,[percentage of value] decimal(5,2)
,[percentage of count_people] decimal(5,2)
)
INSERT INTO #temp VALUES ('Single Gifts',124945,182458.00,1.46,28787034.10,230.40,157.77, null, null)
INSERT INTO #temp VALUES ('New Donor',67598,78799.00,1.17,5915450.93,87.51,75.07, null, null)
INSERT INTO #temp VALUES ('Reactivated',19853,27394.00,1.38,4348419.38,219.03,158.74, null, null)
INSERT INTO #temp VALUES ('2yrs consecutive',20604,31633.00,1.54,3556766.75,172.63,112.44, null, null)
INSERT INTO #temp VALUES ('3/4yrs consecutive',7536,13251.00,1.76,1827917.34,242.56,137.95, null, null)
INSERT INTO #temp VALUES ('5+yrs consecutive',9354,31381.00,3.35,13138479.70,1404.58,418.68, null, null)

您可以运行以下更新来保存每行的百分比:

WITH cte AS
    ( SELECT category,
    value05 / SUM(CASE category WHEN 'Single Gifts' THEN value05 ELSE 0.0 END)
        OVER (PARTITION BY 1) AS [percentage of value],
    count_people / SUM(CASE category WHEN 'Single Gifts' THEN count_people ELSE 0.0 END)
        OVER (PARTITION BY 1) AS [percentage of count_people]
    FROM #temp
    )

UPDATE #temp
SET [percentage of value] = cte.[percentage of value],
    [percentage of count_people] = cte.[percentage of count_people]
FROM #temp
JOIN cte ON #temp.category = cte.category

【讨论】:

感谢您的支持 - 我没想到会按 1 组将类别放在一个分区中【参考方案2】:

在第二个查询中使用第一个作为输入。不要尝试修改第一个查询来添加这些计算,它已经足够复杂了。

【讨论】:

以上是关于百分比行占总数,其中每行由 group by 子句确定的主要内容,如果未能解决你的问题,请参考以下文章

SQL GROUP BY子句,使用AVG在DATESPAN计算中获得浮点数2精度

15group by子句与聚合函数

SQL 子句“GROUP BY 1”是啥意思?

group by / order by 子句有特殊名称吗?

与 GROUP BY 子句一起使用时,如何修复“ORA-00932:不一致的数据类型:预期 - 得到 CLOB”?

加入4个表with group by,2 having和where子句