56,679
社区成员
发帖
与我相关
我的任务
分享
数据表如下:(实际记录很多,上千万条)
drop table if exists tmpt;
Create temporary table TestBase (intime datetime, id int,val1 int,val2 int);
Insert into tmpt values('2000-1-1' , 1,100,110);
Insert into tmpt values('2000-1-1' , 1,500,110);
Insert into tmpt values('2003-1-1' , 1,120,130);
Insert into tmpt values('2005-1-1' , 1,140,150);
Insert into tmpt values('2000-1-1' , 2,160,170);
Insert into tmpt values('2001-1-1' , 2,180,190);[code=SQL]
//创建测试表
drop table if exists tmpt;
Create temporary table tmpt (intime datetime, id int,val1 int,val2 int);
Insert into tmpt values('2000-1-1' , 1,100,110);
Insert into tmpt values('2000-1-1' , 1,500,110);
Insert into tmpt values('2003-1-1' , 1,120,130);
Insert into tmpt values('2005-1-1' , 1,140,150);
Insert into tmpt values('2000-1-1' , 2,160,170);
Insert into tmpt values('2001-1-1' , 2,180,190);
Insert into tmpt values('2004-1-1' , 2,200,210);
drop table if exists calendar;
create table calendar(cdate datetime , constraint pk_calendar primary key (cdate));
insert into calendar value ('2000-01-01');
insert into calendar value ('2000-01-02');
insert into calendar value ('2000-01-03');
..
insert into calendar value ('2007-12-31');
select year(calendar.cdate),
1 as id1, ifnull(avg(IF(id=1,VAL1,null)),0) as avg1,
2 as id2, ifnull(sum(IF(id=2,VAL1,null)),0) as sum1,
1 as id3, ifnull(max(IF(id=1,VAL1,null)),0) as max1
from calendar left join tmpt on date(calendar.cdate)=date(tmpt.intime)
where cdate between '2000-1-1 0:0:0' and '2008-1-1 23:59:59'
group by year(calendar.cdate)
select btime,1,sum(AVG1),2,sum(SUM1),1,sum(MAX1) from
(
select year(cdate) as btime,0 as id1,0 as AVG1,0 as id2,0 as SUM1,0 as id3,0 as MAX1 from calendar where cdate between '2000-1-1 0:0:0' and '2008-1-1 23:59:59' group by btime
union
(select year(intime) AS btime,
1 AS id1,avg(IF(id=1,VAL1,null)) as AVG1,
2 AS id2,sum(IF(id=2,VAL1,null)) as SUM1,
1 AS id3,max(IF(id=1,VAL1,null)) as MAX1
from tmpt where intime between '2000-1-1 0:0:0' and '2008-1-1 23:59:59' group by btime)
) T
group by T.btime
select DATE_FORMAT(intime,'%Y-%m'),
1 as id1,ifnull(sum(IF(id=1,VAL1,0))/sum(IF(id=1,1,0)),0) as avg1,
2 as id2,ifnull(sum(IF(id=2,VAL2,0))/sum(IF(id=2,1,0)),0) as avg2
from calendar left join tmpt on calendar.cdate=tmpt.intime
group by DATE_FORMAT(intime,'%Y-%m')
create table calendar(
cdate datetime ,
constraint pk_calendar primary key (cdate)
)
insert into calendar value ('2000-01-01');
insert into calendar value ('2000-01-02');
insert into calendar value ('2000-01-03');
..
insert into calendar value ('2007-12-31');
mysql> select YEAR(intime),
-> 1 as id1,ifnull(sum(IF(id=1,VAL1,0))/sum(IF(id=1,1,0)),0) as avg1,
-> 2 as id2,ifnull(sum(IF(id=2,VAL2,0))/sum(IF(id=2,1,0)),0) as avg2
-> from calendar left join tmpt on calendar.cdate=tmpt.intime
-> group by YEAR(intime);
+--------------+-----+--------+-----+--------+
| YEAR(intime) | id1 | avg1 | id2 | avg2 |
+--------------+-----+--------+-----+--------+
| 2000 | 1 | 300.00 | 2 | 170.00 |
| 2000 | 1 | 300.00 | 2 | 170.00 |
| 2001 | 1 | 0.00 | 2 | 190.00 |
| 2003 | 1 | 120.00 | 2 | 0.00 |
| 2004 | 1 | 0.00 | 2 | 210.00 |
| 2005 | 1 | 140.00 | 2 | 0.00 |
| 2006 | 1 | 0.00 | 2 | 0.00 |
| 2007 | 1 | 0.00 | 2 | 0.00 |
+--------------+-----+--------+-----+--------+
1 row in set (0.00 sec)
mysql>
select YEAR(intime),
1 as id1,ifnull(sum(IF(id=1,VAL1,0))/sum(IF(id=1,1,0)),0) as avg1,
2 as id2,ifnull(sum(IF(id=2,VAL2,0))/sum(IF(id=2,1,0)),0) as avg2
from tmpt
group by YEAR(intime)
mysql> select * from tmpt;
+---------------------+------+------+------+
| intime | id | val1 | val2 |
+---------------------+------+------+------+
| 2000-01-01 00:00:00 | 1 | 100 | 110 |
| 2000-01-01 00:00:00 | 1 | 500 | 110 |
| 2003-01-01 00:00:00 | 1 | 120 | 130 |
| 2005-01-01 00:00:00 | 1 | 140 | 150 |
| 2000-01-01 00:00:00 | 2 | 160 | 170 |
| 2001-01-01 00:00:00 | 2 | 180 | 190 |
| 2004-01-01 00:00:00 | 2 | 200 | 210 |
+---------------------+------+------+------+
7 rows in set (0.00 sec)
mysql> select YEAR(intime),1 as id1,ifnull(sum(IF(id=1,VAL1,0))/sum(IF(id=1,1,0)),0) as avg1,
-> 2 as id2,ifnull(sum(IF(id=2,VAL2,0))/sum(IF(id=2,1,0)),0) as avg2
-> from tmpt
-> group by YEAR(intime);
+--------------+-----+--------+-----+--------+
| YEAR(intime) | id1 | avg1 | id2 | avg2 |
+--------------+-----+--------+-----+--------+
| 2000 | 1 | 300.00 | 2 | 170.00 |
| 2001 | 1 | 0.00 | 2 | 190.00 |
| 2003 | 1 | 120.00 | 2 | 0.00 |
| 2004 | 1 | 0.00 | 2 | 210.00 |
| 2005 | 1 | 140.00 | 2 | 0.00 |
+--------------+-----+--------+-----+--------+
5 rows in set (0.00 sec)