SQL按时间段分组查询统计、累计语句

#按照日期分组统计查询
SELECT 
        date_format(a.F_CREATE_TIME,"%Y-%m-%d") as time,count(distinct a.F_ID) as newcount,count(distinct b.F_ID) as bnewcount,count(distinct c.F_MSR_ID) as cnewcount  
FROM
        `t_zcgl_user` a 
LEFT JOIN 
    `t_qb_gr_smrz` b ON date_format(a.F_CREATE_TIME,"%Y-%m-%d") = date_format(b.F_CREATE_TIME,"%Y-%m-%d")
LEFT JOIN 
    `t_zcgl_total_record` c ON date_format(a.F_CREATE_TIME,"%Y-%m-%d") = date_format(c.F_CREATE_TIME,"%Y-%m-%d")
group by date_format(a.F_CREATE_TIME,"%Y-%m-%d") 

 

#按照月分组统计查询

SELECT 
        date_format(F_CREATE_TIME,"%Y-%m") as time,count(distinct F_ID) as newcount 
FROM
        `t_qb_gr_smrz`
group by date_format(F_CREATE_TIME,"%Y-%m");

#解析时间戳按照月份
SELECT from_unixtime(F_CREATE_TIME,"%Y-%m") as time,count(distinct F_ID) as newcount FROM `t_zcgl_user` group by from_unixtime(F_CREATE_TIME,"%Y-%m");

 

#按月分组累计查询
SELECT
    s1.months, sum( s2.num ) as total 
FROM
    (
        SELECT DATE_FORMAT(F_CREATE_TIME, '%Y-%m-%d') AS months, count(DISTINCT F_ID) AS num
        FROM t_zcgl_user 
        GROUP BY months
    ) s1
JOIN
    (
        SELECT DATE_FORMAT(F_CREATE_TIME, '%Y-%m-%d') AS months, count(DISTINCT F_ID) AS num
        FROM t_zcgl_user 
        GROUP BY months
    ) s2 

WHERE  s1.months >= s2.months AND s1.months <= '2020-12' AND s1.months >= '2021-12'
GROUP BY s1.months;

 

#累计求和查询

SELECT SUM(a.F_ORDER_AMOUNT) AS zcOrderTotalPrice
FROM t_zcgl_zc_order a
WHERE DATE_FORMAT(a.F_CREATE_TIME, '%Y-%m') <= '2020-11'

  • 1
    点赞
  • 4
    收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:编程工作室 设计师:CSDN官方博客 返回首页
评论

打赏作者

蜡筆尛賢

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

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

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

打赏作者

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

抵扣说明:

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

余额充值