首页 > 代码库 > MySQL学习_计算用户支付方式占比_20161104

MySQL学习_计算用户支付方式占比_20161104

计算用户支付方式占比

SELECT b.*#根据城市ID 年月排序
FROM (

    SELECT a.*
    FROM ( #纵向合并
        SELECT b1.ID,a1.城市,a1.收款方式,DATE_FORMAT(a1.确认时间,"%Y%m") AS 年月,SUM(a1.金额) AS 收款方式金额
        FROM `a029_collectionment` AS a1
        LEFT JOIN `a000_city` AS b1 ON a1.城市=b1.城市
        WHERE a1.确认时间>"2016-01-01" AND a1.确认时间<CURRENT_DATE AND a1.收款方式="现金"
        GROUP BY a1.城市,DATE_FORMAT(a1.确认时间,"%Y%m")
    ) AS a
    UNION ALL
    (
        SELECT d1.ID,c1.城市,c1.收款方式,DATE_FORMAT(c1.支付时间,"%Y%m") AS 年月,SUM(c1.金额) AS 收款方式金额
        FROM `a031_order_paypal` AS c1
        LEFT JOIN  `a000_city` AS d1 ON d1.城市=c1.城市
        WHERE c1.支付时间>"2016-01-01" AND c1.支付时间<CURRENT_DATE
        GROUP BY c1.城市,c1.收款方式,DATE_FORMAT(c1.支付时间,"%Y%m")
    )
) AS b
ORDER BY b.ID,b.年月

 

MySQL学习_计算用户支付方式占比_20161104