MySQL Monthly by week statistics

Source: Internet
Author: User

Http://hi.baidu.com/liangjian1024/blog/item/6861541b1416094842a9ad7c.html

Table finance has two fields as follows
Date Date
Money Double (15,2)
The following need for the 2010 financial data of the table finance, monthly statistics

SQL code
  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


Here are the weekly statistics
View MySQL's Manual
%x year for the week where Sunday are the first day of the week, numeric, and four digits; Used with%V
%x year for the week, where Monday are the first day of the week, numeric, and four digits; Used with%v
which
1 if Monday is the first day of the week, then (lowercase) date_format (DATE, '%x%v ')
2 If Sunday is the first day of the week, then (uppercase) Date_format (DATE, '%x%V ')

SQL code
      1. Select Date_format (date,'%x year-%v Week ') as week,sum(Money) as money
      2. from Finance_base
      3. where Date_format (date,'%Y ') =2010
      4. Group by week

MySQL Monthly by week statistics

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.