-- 此select按平台名称分组 以当时扫码次数倒序,全部扫码次数倒序
SELECT
*,
sum( anyDayScanNum ) AS allScanNum
FROM
(
-- 此select按每日分组
SELECT
distributor_name,
distributor_code,
-- 所有日的扫码次数
sum( prod_num ) AS anyDayScanNum,
-- 扫码日期格式化 便于按日期分组
date_format( ddate, '%Y%m%d' ) AS dateformat,
-- 以下判断为当日则展示当日扫码次数,非当日则置为0
CASE
WHEN date_format( ddate, '%Y%m%d' ) = date_format( now( ), '%Y%m%d' ) THEN
sum( prod_num ) ELSE 0
END AS dayScanNum
FROM
-- 订单拆分表
u_order_split
WHERE
-- 订单查询条件
order_source = 1
AND order_direct = 1
AND order_type = '0'
GROUP BY
-- 安日期分组,平台名称分组
dateformat,
distributor_name
ORDER BY
-- 按日期倒序排,便于外层分组时留下当日扫码次数
dateformat DESC,
dayScanNum DESC
) AS bb
GROUP BY
distributor_name
ORDER BY
dayScanNum DESC,
allScanNum DESC