码迷,mamicode.com
首页 > 其他好文 > 详细

按天去除重复数据,为0则取0,否则取最大的那个值

时间:2014-07-13 13:27:58      阅读:237      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   数据   os   2014   

测试数据:
mysql> select * from t2;
+----+--------+---------------------+------------+
| id | userid | inputDate           | infoStatus |
+----+--------+---------------------+------------+
|  1 |      1 | 2014-07-11 00:00:00 |      20013 | 
|  2 |      1 | 2014-07-11 00:00:00 |          0 | 
|  3 |      2 | 2014-07-12 00:00:11 |      20015 | 
|  4 |      2 | 2014-07-12 00:00:22 |      20013 | 
|  5 |      2 | 2014-07-14 00:00:00 |          0 | 
|  6 |      2 | 2014-07-15 00:00:00 |      20013 | 
|  7 |      2 | 2014-07-15 00:00:00 |          0 | 
|  8 |      2 | 2014-07-16 00:00:00 |          0 | 
|  9 |      2 | 2014-07-13 00:00:00 |      20013 | 
| 10 |      2 | 2014-07-13 00:00:00 |          0 | 
| 11 |      2 | 2014-07-14 00:00:00 |          0 | 
+----+--------+---------------------+------------+

查询sql:

select id,userid,date(inputDate) as date,max(infoStatus) as m,min(infoStatus) as i,if(min(infoStatus)=0,0,max(infoStatus)) as max from t2 where infoStatus = 0 or infoStatus <= 99999 group by date;

回执结果:
+----+--------+------------+-------+-------+-------+
| id | userid | date       | m     | i     | max   |
+----+--------+------------+-------+-------+-------+
|  1 |      1 | 2014-07-11 | 20013 |     0 |     0 | 
|  3 |      2 | 2014-07-12 | 20015 | 20013 | 20015 | 
|  9 |      2 | 2014-07-13 | 20013 |     0 |     0 | 
|  5 |      2 | 2014-07-14 |     0 |     0 |     0 | 
|  6 |      2 | 2014-07-15 | 20013 |     0 |     0 | 
|  8 |      2 | 2014-07-16 |     0 |     0 |     0 | 
+----+--------+------------+-------+-------+-------+

我的思路是按照时间分组,并同时查询最小,跟最大的,如果最小的为0,那max字段就为0,否则就取最大的那个

 

按天去除重复数据,为0则取0,否则取最大的那个值,布布扣,bubuko.com

按天去除重复数据,为0则取0,否则取最大的那个值

标签:style   blog   color   数据   os   2014   

原文地址:http://www.cnblogs.com/shibazi/p/3840774.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!