将总和转换为百分比

Posted

技术标签:

【中文标题】将总和转换为百分比【英文标题】:Convert Total Sum Into Percentage 【发布时间】:2019-04-26 06:26:50 【问题描述】:

我想将总和结果转换为百分比。添加后,总共将产生 100% 的结果。我已经按备注对它进行了分组,并且已经从我的查询中得到了总数。但它仍然在数量上。

这是我的查询

SELECT
    CASE
        WHEN A.district = B.district THEN 'Delivered from store in same district'
        WHEN A.city = B.city THEN 'Delivered from store in same city'
        WHEN A.province = B.province THEN 'Delivered from store in same province'
        ELSE 'Not delivered from nearest store'
    END AS 'REMARK',
    SUM(CASE
        WHEN YEAR(T1.create_time) = '2019' AND MONTH(T1.create_time) = '01' THEN 1
        ELSE 0
    END) AS 'Jan-19',
    SUM(CASE
        WHEN YEAR(T1.create_time) = '2019' AND MONTH(T1.create_time) = '02' THEN 1
        ELSE 0
    END) AS 'Feb-19',
    SUM(CASE
        WHEN YEAR(T1.create_time) = '2019' AND MONTH(T1.create_time) = '03' THEN 1
        ELSE 0
    END) AS 'Mar-19', 
    SUM(CASE
        WHEN YEAR(T1.create_time) = '2019' AND MONTH(T1.create_time) = '04' THEN 1
        ELSE 0
    END) AS 'Apr-19'
FROM 
    au_store_add A
INNER JOIN
    (SELECT
        A.id,
        A.code,
        A.create_time,
        A.plat_create_time,
        A.appoint_stcode,
        A.status
        FROM deli_order A
        WHERE A.appoint_stcode IS NOT NULL
    UNION
        SELECT
        B.id,
        B.code,
        B.create_time,
        B.plat_create_time,
        B.destination_code,
        B.status
        FROM deli_order B
        WHERE B.destination_code IS NOT NULL) T1 ON T1.appoint_stcode = A.store_code
INNER JOIN
    deli_order_delivery B ON B.order_id = T1.id
WHERE 
    YEAR(T1.plat_create_time)='2019' and T1.status = 6 and (month(T1.plat_create_time) in (1,2,3,4))
GROUP BY
    CASE
        WHEN A.district = B.district THEN 'Delivered from store in same district'
        WHEN A.city = B.city THEN 'Delivered from store in same city'
        WHEN A.province = B.province THEN 'Delivered from store in same province'
        ELSE 'Not delivered from nearest store'
    END

结果是这样的

REMARK                                | Jan-19| Feb-19| Mar-19| Apr-19|
-----------------------------------------------------------------------
Delivered from store in same city        252      198    308      283
Delivered from store in same district    114      110    163      138 
Delivered from store in same province    1135     976    1387    1208
Not delivered from nearest store         3046     2518   3189    3123

我需要为每个月生成这样的东西(没有“总计”)

REMARK                                | Jan-19| 
-----------------------------------------------
Delivered from store in same city        5.5%     
Delivered from store in same district    2.5%     
Delivered from store in same province    25%     
Not delivered from nearest store         67%    
Grand Total                              4547  (100%)   

我应该在查询中添加什么?

【问题讨论】:

你用的是什么版本的 mysql @TimBiegeleisen 我正在使用 MySQL 8.0.13 【参考方案1】:

鉴于您使用的是 MySQL 8+,那么 COUNT() 作为分析函数应该可以使用。它在这里派上用场:

SELECT
    CASE
        WHEN A.district = B.district THEN 'Delivered from store in same district'
        WHEN A.city = B.city THEN 'Delivered from store in same city'
        WHEN A.province = B.province THEN 'Delivered from store in same province'
        ELSE 'Not delivered from nearest store'
    END AS REMARK,
    100.0 * COUNT(CASE WHEN DATE_FORMAT(create_time, '%Y-%m') = '2019-01' THEN 1 END)
        / COUNT(CASE WHEN DATE_FORMAT(create_time, '%Y-%m') = '2019-01'
                     THEN 1 END) OVER () `Jan-19`,
    100.0 * COUNT(CASE WHEN DATE_FORMAT(create_time, '%Y-%m') = '2019-02' THEN 1 END)
        / COUNT(CASE WHEN DATE_FORMAT(create_time, '%Y-%m') = '2019-02'
                     THEN 1 END) OVER () `Feb-19`,
    100.0 * COUNT(CASE WHEN DATE_FORMAT(create_time, '%Y-%m') = '2019-03' THEN 1 END)
        / COUNT(CASE WHEN DATE_FORMAT(create_time, '%Y-%m') = '2019-03'
                     THEN 1 END) OVER () `Mar-19`,
    100.0 * COUNT(CASE WHEN DATE_FORMAT(create_time, '%Y-%m') = '2019-04' THEN 1 END)
        / COUNT(CASE WHEN DATE_FORMAT(create_time, '%Y-%m') = '2019-04'
                     THEN 1 END) OVER () `Apr-19`
FROM 
    au_store_add A
...

我使用上面的DATE_FORMAT 使年份和月份的检查不那么冗长。我还用COUNT 代替了SUM,这让我们可以使用稍微短一些的条件表达式。

【讨论】:

我试试你的查询,但似乎百分比有点偏。在“1 月 19 日”和备注“未从最近的商店发货”有 3046 条记录。它应该比当月的总记录高出 50% 以上。 问题:您想要整个 2019 年的百分比,还是想要标准化为仅从 1 月到 4 月?这些不是一回事,也不是同一个查询。 每月 100% 我想标准化为仅从 1 月到 4 月 我尝试查询并手动汇总百分比,似乎百分比总数是按备注分组的,而不是按月份分组的。我应该通过查询更改我的组中的某些内容吗?

以上是关于将总和转换为百分比的主要内容,如果未能解决你的问题,请参考以下文章

arcgis中的加权叠加怎么做

列 SQL 百分比的总和

用于计算同一列的值百分比的 SQL [关闭]

将像素转换为百分比

将整数转换为百分比

如何使用 SASS 将数字转换为百分比? [复制]