我想将总和结果转换为百分比。添加后,总计将达到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%)
我应该在查询中添加什么?
答案 0 :(得分:0)
鉴于您正在使用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
,这使我们可以使用略短的条件表达式。