程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> MYSQL數據庫 >> MySQL綜合教程 >> mysql跨年按周分組

mysql跨年按周分組

編輯:MySQL綜合教程

mysql跨年按周分組

Sql代碼  
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('2010-12-31',100);  
insert into sales (date,cost)values('2011-01-01',200);  
insert into sales (date,cost)values('2011-01-02',100);  
insert into sales (date,cost)values('2011-01-06',100);  
insert into sales (date,cost)values('2011-01-10',100);  
  
SELECT * FROM employees.sales;  
  
select week(date),sum(cost)from sales group by week(date);  
  
select floor(datediff(date,'1900-01-01')/7) as a,sum(cost) from sales group by floor(datediff(date,'1900-01-01')/7);  
  
/*最終解決方案*/  
select date_add('1900-01-01',interval floor(datediff(date,'1900-01-01')/7)*7 day) as week_start,  
date_add('1900-01-01',interval floor(datediff(date,'1900-01-01')/7)*7+6 day)as week_end,sum(cost) from sales  
group by floor(datediff(date,'1900-01-01')/7);  

 

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved