Mysql groups by week across years

Source: Internet
Author: User

Mysql groups by week across years

SQL code use employees; create table sales (id int auto_increment, date datetime not null, cost int unsigned not null, primary key (id); insert into sales (date, cost) values ('2017-12-31 ', 2010); insert into sales (date, cost) values ('2017-01-01', 100); insert into sales (date, cost) values ('2017-01-02 ', 2011); insert into sales (date, cost) values ('2017-01-06', 100); insert into sales (date, cost) values ('2014-01-10 ', 2011); SELECT * FROM employees. sales; select week (date), sum (cost) from sales group by week (date); select floor (datediff (date, '2017-01-01 ')/7) as, sum (cost) from sales group by floor (datediff (date, '2017-01-01 ')/7);/* Final Solution */select date_add ('2017-01-01 ', interval floor (datediff (date, '1970-01-01 ')/7) * 7 day) as week_start, date_add ('1970-01-01', interval floor (datediff (date, '2017-01-01 ')/7) * 7 + 6 days) as week_end, sum (cost) from sales group by floor (datediff (date, '2017-01-01 ') /7 );

 

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.