随笔 - 3065  文章 - 0 评论 - 0 阅读 - 735万
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

转自:https://www.freesion.com/article/1415317089/

 

mysql中关于分组的使用

参考这个使用

  1.  
    select DATE_FORMAT(create_time,'%Y%u') weeks,count(caseid) count from tc_case group by weeks;
  2.  
    select DATE_FORMAT(create_time,'%Y%m%d') days,count(caseid) count from tc_case group by days;
  3.  
    select DATE_FORMAT(create_time,'%Y%m') months,count(caseid) count from tc_case group by months;
  4.  
     
  5.  
    1、按照月份:
  6.  
    select sum(total_amount) as total, date_format(stat_date, '%Y-%m') from week_report WHERE `stat_date` BETWEEN '2016-11-02' AND '2017-04-30' group by date_format(stat_date, '%Y-%m');
  7.  
    select sum(total_amount) as total,date_format(stat_date, '%Y-%m') from week_report WHERE `stat_date` BETWEEN '2016-12-11' AND '2016-12-22' group by date_format(stat_date, '%Y-%m');
  8.  
    获得按照月份分组进行汇总的数据。
  9.  
     
  10.  
    concat()连接字符串
  11.  
     
  12.  
    -- month
  13.  
    select CONCAT(YEAR(stat_date),'_',DATE_FORMAT(stat_date,'%m')) months ,sum(total_amount) as count_amount, sum(total_new_user) as count_new_user, sum(da_active_user) as count_active_user from xxx
  14.  
    WHERE `stat_date` BETWEEN '2016-01-02' AND '2017-05-30' group by months;
  15.  
     
  16.  
    -- 季度
  17.  
    select CONCAT(YEAR(stat_date),'_',quarter(stat_date)) qu,sum(total_amount) as count_amount, sum(total_new_user) as count_new_user, sum(da_active_user) as count_active_user from xxx
  18.  
    WHERE `stat_date` BETWEEN '2016-01-02' AND '2017-05-30' group by qu;
  19.  
     
  20.  
    -- 周
  21.  
    select CONCAT(YEAR(stat_date),'_',DATE_FORMAT(stat_date,'%U')) weeks,sum(total_amount) as count_amount, sum(total_new_user) as count_new_user, sum(da_active_user) as count_active_user from xxx
  22.  
    WHERE `stat_date` BETWEEN '2016-01-02' AND '2017-05-30' group by weeks;
  23.  
     
  24.  
    -- 天
  25.  
    select CONCAT(YEAR(stat_date),'_',DATE_FORMAT(stat_date,'%m'),'_',DATE_FORMAT(stat_date,'%d')) days, sum(total_amount) as count_amount, sum(total_new_user) as count_new_user, sum(da_active_user) as count_active_user from xxx
  26.  
    WHERE `stat_date` BETWEEN '2016-01-02' AND '2017-05-30' group by days
posted on   Sharpest  阅读(2251)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
历史上的今天:
2019-06-29 Oracle中select 1和select *的区别
2016-06-29 mybatis No enum const class org.apache.ibatis.type.JdbcType.Date 坑爹的配置
2016-06-29 Spring Test 整合 JUnit 4 使用总结
2016-06-29 论坛中常有的提问,评论,点赞设计
点击右上角即可分享
微信分享提示