MySQL从多个记录中选择仅包含第一个和最后一个日期的记录

时间:2015-07-22 11:13:44

标签: mysql sql

我有一张表HC_RSPI_data

| RADIO_TYPE |   HOP | TOWARDS | RSPI |       TP_NAME |          DATE_TIME_END | TIME_LENGTH | RLTM_min | RLTM_max |
|------------|-------|---------|------|---------------|------------------------|-------------|----------|----------|
| FlexiMetro | H3376 | RAPSANI | RSPI | ODU 1 Radio 1 | June, 08 2015 00:00:00 |       86400 |       65 |       37 |
| FlexiMetro | H3376 | RAPSANI | RSPI | ODU 1 Radio 1 | June, 09 2015 00:00:00 |       86400 |       70 |       37 |
| FlexiMetro | H3376 | RAPSANI | RSPI | ODU 1 Radio 1 | June, 08 2015 00:00:00 |       86400 |       65 |       37 |
| FlexiMetro | H3376 | RAPSANI | RSPI | ODU 2 Radio 2 | June, 10 2015 00:00:00 |       86400 |       74 |       47 |
| FlexiMetro | H3376 | RAPSANI | RSPI | ODU 2 Radio 2 | June, 09 2015 00:00:00 |       86400 |       74 |       47 |
| FlexiMetro | H3376 | RAPSANI | RSPI | ODU 2 Radio 2 | June, 08 2015 00:00:00 |       86400 |       74 |       47 |

我希望在第一天和最后一天获得记录,如:

| RADIO_TYPE |   HOP | TOWARDS | RSPI |       TP_NAME |          DATE_TIME_END | TIME_LENGTH | RLTM_min | RLTM_max |
|------------|-------|---------|------|---------------|------------------------|-------------|----------|----------|
| FlexiMetro | H3376 | RAPSANI | RSPI | ODU 1 Radio 1 | June, 08 2015 00:00:00 |       86400 |       65 |       37 |
| FlexiMetro | H3376 | RAPSANI | RSPI | ODU 1 Radio 1 | June, 10 2015 00:00:00 |       86400 |       70 |       37 |

| FlexiMetro | H3376 | RAPSANI | RSPI | ODU 2 Radio 2 | June, 08 2015 00:00:00 |       86400 |       74 |       47 |
| FlexiMetro | H3376 | RAPSANI | RSPI | ODU 2 Radio 2 | June, 10 2015 00:00:00 |       86400 |       74 |       47 |

OR

一个包含2个文件的记录开始日期和结束日期如下: min(DATE_TIME_END)为开始日期,max(DATE_TIME_END)为结束日期

我怎样才能做到这一点? 我正在尝试这样的事情

SELECT DISTINCT rssi.* 
FROM HC_RSPI_data as rssi
INNER JOIN HC_RSPI_data as j
on  rssi.RADIO_TYPE     =   j.RADIO_TYPE     
and rssi.HOP            =   j.HOP            
and rssi.TOWARDS        =   j.TOWARDS        
and rssi.RSPI           =   j.RSPI           
and rssi.TP_NAME        =   j.TP_NAME
and rssi.DATE_TIME_END = j.DATE_TIME_END or j.DATE_TIME_END = DATE_ADD(rssi.DATE_TIME_END, INTERVAL 6 DAY)

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

一种方法是聚合数据并使用join来获取适当的值:

SELECT rssi.* 
FROM HC_RSPI_data rssi INNER JOIN
     (SELECT RADIO_TYPE, HOP, TOWARDS, RSPI, TP_NAME,
             MIN(DATE_TIME) as mindt, MAX(DATE_TIME) maxdt
      FROM HC_RSPI_data
      GROUP BY RADIO_TYPE, HOP, TOWARDS, RSPI, TP_NAME
     ) j
     ON  rssi.RADIO_TYPE     =   j.RADIO_TYPE and
         rssi.HOP            =   j.HOP and
         rssi.TOWARDS        =   j.TOWARDS and       
         rssi.RSPI           =   j.RSPI and    
         rssi.TP_NAME        =   j.TP_NAME
         rssi.DATE_TIME in (j.mindt, j.maxdt);

答案 1 :(得分:0)

获取包含开始日期和结束日期的一行:

SELECT rssi.RADIO_TYPE, rssi.HOP, rssi.TOWARDS, rssi.RSPI, rssi.TP_NAME,
 MIN(rssi.DATE_TIME_END) as mindt, MAX(rssi.DATE_TIME_END) as maxdt
 FROM HC_RSPI_data as rssi 
 GROUP BY rssi.RADIO_TYPE, rssi.HOP, rssi.TOWARDS, rssi.RSPI, rssi.TP_NAME