Mysql统计数据,数据为空补0

之前项目中做了些根据特定时间做报表统计的功能,这里简单记录下,希望能帮到不会的同学。

1.根据指定年份,统计某年全年每月的数据,数据为空补0。(没有为系统维护时间表情况 create_time为时间戳  毫秒级别

 sql如下:

SELECT
        a.`month` AS `month`,
        ifnull( b.backAmtStr, '0.00' ) as backAmtStr,
        ifnull( b.finishedAmtStr, '0.00' ) as finishedAmtStr,
        ifnull( b.invoiceAmtStr, '0.00' ) as invoiceAmtStr
        FROM
            (
        SELECT
            CONCAT(#{date},'-01') AS `month` UNION
        SELECT
            CONCAT(#{date},'-02') AS `month` UNION
        SELECT
            CONCAT(#{date},'-03') AS `month` UNION
        SELECT
            CONCAT(#{date},'-04') AS `month` UNION
        SELECT
            CONCAT(#{date},'-05') AS `month` UNION
        SELECT
            CONCAT(#{date},'-06') AS `month` UNION
        SELECT
            CONCAT(#{date},'-07') AS `month` UNION
        SELECT
            CONCAT(#{date},'-08') AS `month` UNION
        SELECT
            CONCAT(#{date},'-09') AS `month` UNION
        SELECT
            CONCAT(#{date},'-10') AS `month` UNION
        SELECT
            CONCAT(#{date},'-11') AS `month` UNION
        SELECT
            CONCAT( #{date},'-12') AS `month`
            ) a
            LEFT JOIN (
        SELECT
            from_unixtime( tc.create_time / 1000, '%Y-%m' ) AS `month`,
            CAST( IFNULL( sum( tcs.order_amt ), 0 ) / 100 AS DECIMAL ( 20, 2 ) ) AS backAmtStr,
            CAST( IFNULL( sum( tcs.order_in_amount ), 0 ) / 100 AS DECIMAL ( 20, 2 ) ) AS finishedAmtStr,
            CAST( IFNULL( sum( tcs.invoice_amount ), 0 ) / 100 AS DECIMAL ( 20, 2 ) ) AS invoiceAmtStr
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

纯洁的一笑

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值