如何考虑列中的数据流来汇总表格

时间:2015-09-11 21:23:26

标签: mysql sql distinct

考虑下表:

|id | region| name  | date       |
|1  | 10    | JHONY | 2015-08-21 |
|2  | 10    | JHONY | 2015-08-21 |
|3  | 20    | JHONY | 2015-08-21 |
|4  | 10    | JHONY | 2015-08-21 |
|5  | 10    | JHONY | 2015-08-21 |
|6  | 10    | MARY  | 2015-08-21 |
|7  | 10    | MARY  | 2015-08-21 |
|8  | 20    | MARY  | 2015-08-21 |
|9  | 10    | MARY  | 2015-08-21 |
|10 | 10    | JHONY | 2015-08-21 |
|11 | 10    | JHONY | 2015-08-22 |

我需要总结一下,忽略区域,名称和日期的重复,当在该日期更改同名的区域时,必须考虑一次出现。在这种情况下,结果将是:

| region| name  | date       |
| 10    | JHONY | 2015-08-21 |
| 20    | JHONY | 2015-08-21 |
| 10    | JHONY | 2015-08-21 |
| 10    | MARY  | 2015-08-21 |
| 20    | MARY  | 2015-08-21 |
| 10    | MARY  | 2015-08-21 |
| 10    | JHONY | 2015-08-22 |

我尝试使用

select distinct region, name, DATE_FORMAT(date, '%Y-%m-%d') from new_table;

可以在http://sqlfiddle.com/#!2/73c1f3/2

中看到

但由于区域的变化,它隐藏了#4和#5行(应该产生单行)和#9(应该显示)。是否可以使用MySql?

1 个答案:

答案 0 :(得分:1)

您需要获取上一个区域。我会用相关的子查询来解决这个问题:

select t.name, t.region, t.date
from (select t.*,
             (select t2.region
              from new_table t2
              where t2.name = t.name and
                    t2.date = t.date and -- not sure if this condition is really necessary
                    t2.id < t.id
              order by t2.id desc
              limit 1
             ) as prev_region
      from new_table t
     ) t
where prev_region is null or prev_region <> region;

为了提高性能,我建议在new_table(name, id, region)上建一个索引。

Here是SQL小提琴。