實現mysql按月統計的教程

來源:互聯網
上載者:User

標籤:

From: http://www.jbxue.com/db/758.html

實現mysql按月統計的教程 

mysql有個欄位是DATETIME類型,要實現可以按月統計,該怎麼寫sql語句?
select month(f1) from tt group by month(f1)
or

select DATE_FORMAT(f1,‘%m‘) from tt group by DATE_FORMAT(f1,‘%m‘)

比如資料庫的為2008-01-15 12:10:00
則DATE_FORMAT的參數格式分別得到的結果為:
‘%Y‘ 2008
‘%Y-%m‘ 2008-01
‘%Y-%c‘ 2008-1
‘%m‘ 01
‘%c‘ 1

Date_format格式說明:
格式 描述
%a 縮寫星期名
%b 縮寫月名
%c 月,數值
%D 帶有英文首碼的月中的天
%d 月的天,數值(00-31)
%e 月的天,數值(0-31)
%f 微妙
%H 小時(00-23)
%h 小時(01-12)
%I 小時(01-12)
%i 分鐘,數值(00-59)
%j 年的天(001-366)
%k 小時(0-23)
%l 小時(1-12)
%M 月名
%m 月,數值(00-12)
%p AM或PM
%r 時間,12-小時(hh:mm:ss AM或PM)
%S 秒(00-59)
%s 秒(00-59)
%T 時間, 24-小時(hh:mm:ss)
%U 周(00-53)星期日是一周的第一天
%u 周(00-53)星期一是一周的第一天
%V 周(01-53)星期日是一周的第一天,與%X使用
%v 周(01-53)星期一是一周的第一天,與%x使用
%W 星期名
%w 周的天(0=星期日, 6=星期六)
%X 年,其中的星期日是周的第一天,4位,與%V使用
%x 年,其中的星期一是周的第一天,4位,與%v使用
%Y 年,4位
%y 年,2位

按季度存資料
select YEAR(procurement_dt)*10+((MONTH(procurement_dt)-1) DIV 3) +1, MONTH(procurement_dt) , procurement_dt from xs001
groupbyconcat(date_format(savetime,‘%Y‘),FLOOR((date_format(savetime,‘%m‘)+2)/3))

另外的按月統計方式

MySQL-按月統計資料
統計2010年 每月的資金

select DATE_FORMAT(date,‘%Y-%m‘) as month,sum(money) as money from finance where DATE_FORMAT(date,‘%Y‘)=2010 group by month order by month

1如果周一為一周的第一天,則(小寫) DATE_FORMAT(date,‘%x %v‘)

2如果周日為一周的第一天,則(大寫) DATE_FORMAT(date,‘%X %V‘)

 

統計每個星期

select DATE_FORMAT(date,‘%x年-第%v周‘) as week,sum(money) as money from finance_base where DATE_FORMAT(date,‘%Y‘)=2010 group by week;
select ((year(`recview`.`paytime`) * 100) + month(`recview`.`paytime`)) AS `paytime`,sum(`recview`.`rent`) AS `rent` from `recview` group by
((year(`recview`.`paytime`) * 100) + month(`recview`.`paytime`)) order by ((year(`recview`.`paytime`) * 100) + month(`recview`.`paytime`));

您可能感興趣的文章:
Mysql 按年度、季度、月度、周、日統計查詢的例子
mysql按年度、季度、月度、周、日統計查詢的sql語句
mysql查詢:上周、月、季度、年和本周、月、季度、年

-----------------------------------------------------------------------------------

From: http://phl.iteye.com/blog/717872

表finance有倆個欄位如下
date date
money double(15,2)
    下面需要對錶finance的2010年財務資料,按月進行統計

Sql代碼  
  1. select DATE_FORMAT(date,‘%Y-%m‘) as month,sum(money) as money   
  2. from finance   
  3. where DATE_FORMAT(date,‘%Y‘)=2010   
  4. group by month   
  5. order by month  


    下面是按周統計
查看MySQL的manual
%X Year for the week where Sunday is the first day of the week, numeric, four digits; used with %V
%x Year for the week, where Monday is the first day of the week, numeric, four digits; used with %v
    其中
1如果周一為一周的第一天,則(小寫) DATE_FORMAT(date,‘%x %v‘)
2如果周日為一周的第一天,則(大寫) DATE_FORMAT(date,‘%X %V‘)

Sql代碼  
    1. select DATE_FORMAT(date,‘%x年-第%v周‘) as week,sum(money) as money   
    2. from finance_base   
    3. where DATE_FORMAT(date,‘%Y‘)=2010   
    4. group by week 
  

實現mysql按月統計的教程

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.