SQL在给定的时间段内选择值范围内的记录

时间:2015-07-16 08:01:42

标签: mysql sql database views

我有一个Sql表HC_RSPI,我想选择并显示记录 只有当一个字段的值在一段时间内的范围内时,我们才说 多个记录连续5或7天。

| 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, 10 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 | H3178 | NEAPOLI_LARISSA | RSPI | ODU 1 Radio 1 | June, 10 2015 00:00:00 |       86406 |       37 |       32 |
| FlexiMetro | H3178 | NEAPOLI_LARISSA | RSPI | ODU 2 Radio 2 | June, 10 2015 00:00:00 |       86406 |       48 |       43 |
| FlexiMetro | H3376 |      TYRNAVOS_2 | RSPI | ODU 1 Radio 1 | June, 10 2015 00:00:00 |       86396 |       42 |       36 |
| FlexiMetro | H3376 |      TYRNAVOS_2 | RSPI | ODU 2 Radio 2 | June, 10 2015 00:00:00 |       86396 |       52 |       46 |
| FlexiMetro | H3301 |         LICHADA | RSPI | ODU 1 Radio 1 | June, 10 2015 00:00:00 |       86399 |       58 |       47 |
| FlexiMetro | H3301 |         LICHADA | RSPI | ODU 2 Radio 2 | June, 10 2015 00:00:00 |       86399 |       67 |       56 |
| FlexiMetro | H3390 |        SKIATHOS | RSPI | ODU 1 Radio 1 | June, 10 2015 00:00:00 |       86397 |       63 |       48 |
| FlexiMetro | H3390 |        SKIATHOS | RSPI | ODU 2 Radio 2 | June, 10 2015 00:00:00 |       86397 |       70 |       56 |
| FlexiMetro | H3178 |     LARISSA_MTX | RSPI | ODU 1 Radio 1 | June, 10 2015 00:00:00 |       86400 |       38 |       33 |
| FlexiMetro | H3178 |     LARISSA_MTX | RSPI | ODU 2 Radio 2 | June, 10 2015 00:00:00 |       86400 |       48 |       44 |
| FlexiMetro | H3442 |       ANCHIALOS | RSPI | ODU 1 Radio 1 | June, 10 2015 00:00:00 |       86400 |       53 |       47 |
| FlexiMetro | H3376 |         RAPSANI | RSPI | ODU 1 Radio 1 | June, 09 2015 00:00:00 |       86400 |       70 |       37 |
| FlexiMetro | H3376 |         RAPSANI | RSPI | ODU 2 Radio 2 | June, 09 2015 00:00:00 |       86400 |       74 |       47 |
| 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, 08 2015 00:00:00 |       86400 |       74 |       47 |
| FlexiMetro | H3376 |         RAPSANI | RSPI | ODU 1 Radio 1 | June, 07 2015 00:00:00 |       86400 |       65 |       37 |
| FlexiMetro | H3376 |         RAPSANI | RSPI | ODU 2 Radio 2 | June, 07 2015 00:00:00 |       86400 |       74 |       47 |
| FlexiMetro | H3376 |         RAPSANI | RSPI | ODU 1 Radio 1 | June, 06 2015 00:00:00 |       86400 |       65 |       37 |
| FlexiMetro | H3376 |         RAPSANI | RSPI | ODU 2 Radio 2 | June, 06 2015 00:00:00 |       86400 |       74 |       47 |

我想知道,例如RLTM_min是否在一个值范围内,让我们说65-85在一段时间内让我们说4或5天,这意味着我有问题。 在我们的示例中,H3376 RAPSANI ODU 1无线电1和ODU无线电2连续4天在该范围内(65-85),我只想要这些记录。

输出可能是。

| 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, 10 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 1 Radio 1 | June, 09 2015 00:00:00 |       86400 |       70 |       37 |
| FlexiMetro | H3376 | RAPSANI | RSPI | ODU 2 Radio 2 | June, 09 2015 00:00:00 |       86400 |       74 |       47 |
| 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, 08 2015 00:00:00 |       86400 |       74 |       47 |
| FlexiMetro | H3376 | RAPSANI | RSPI | ODU 1 Radio 1 | June, 07 2015 00:00:00 |       86400 |       65 |       37 |
| FlexiMetro | H3376 | RAPSANI | RSPI | ODU 2 Radio 2 | June, 07 2015 00:00:00 |       86400 |       74 |       47 |
| FlexiMetro | H3376 | RAPSANI | RSPI | ODU 1 Radio 1 | June, 06 2015 00:00:00 |       86400 |       65 |       37 |
| FlexiMetro | H3376 | RAPSANI | RSPI | ODU 2 Radio 2 | June, 06 2015 00:00:00 |       86400 |       74 |       47 |

或者只有带有TOWARDS和TP_NAME的HOP说明哪个有问题。

有没有办法在sql查询中实现它? 我该如何实现?创建另一个表,进行连接?创建一个视图?

谢谢

SQL fiddle

1 个答案:

答案 0 :(得分:0)

select  distinct HC_RSPI_data.*
from    (
        select
            firstDay.RADIO_TYPE
        ,   firstDay.HOP       
        ,   firstDay.TOWARDS   
        ,   firstDay.RSPI      
        ,   firstDay.TP_NAME   
        ,   firstDay.DATE_TIME_END                    as  periodBegins
        ,   (firstDay.DATE_TIME_END + INTERVAL 6 DAY) as  periodEnds
        from    HC_RSPI_data            as  firstDay
            inner join  HC_RSPI_data    as  followingDay
                on  followingDay.RADIO_TYPE     =   firstDay.RADIO_TYPE
                and followingDay.HOP            =   firstDay.HOP            
                and followingDay.TOWARDS        =   firstDay.TOWARDS        
                and followingDay.RSPI           =   firstDay.RSPI           
                and followingDay.TP_NAME        =   firstDay.TP_NAME        
                and followingDay.DATE_TIME_END  between firstDay.DATE_TIME_END and (firstDay.DATE_TIME_END + INTERVAL 6 DAY)
                and (   -- Days when RLTM_min or RLTM_max is between 65-85
                        followingDay.RLTM_min between 65 and 85 
                    or  followingDay.RLTM_max between 65 and 85 
                )
        where   (       -- Days when RLTM_min or RLTM_max is between 65-85
                        firstDay.RLTM_min between 65 and 85 
                    or  firstDay.RLTM_max between 65 and 85 
                )
        group by 
            firstDay.RADIO_TYPE
        ,   firstDay.HOP       
        ,   firstDay.TOWARDS   
        ,   firstDay.RSPI      
        ,   firstDay.TP_NAME   
        ,   firstDay.DATE_TIME_END
        having  sum(1) = 7  -- a period of 7 days with matching values
    )   as  period
    inner join HC_RSPI_data
        on  HC_RSPI_data.RADIO_TYPE      =   period.RADIO_TYPE     
        and HC_RSPI_data.HOP             =   period.HOP            
        and HC_RSPI_data.TOWARDS         =   period.TOWARDS        
        and HC_RSPI_data.RSPI            =   period.RSPI           
        and HC_RSPI_data.TP_NAME         =   period.TP_NAME        
        and HC_RSPI_data.DATE_TIME_END    between period.periodBegins and period.periodEnds
;