Search code examples
sqlsqlplushistorianaspen

How can I select separate time intervals simultaneously in SQL query?


Below is a section of my SQL query. I want to select data from the time periods shown. I have many specific date ranges I need to include in the 'or' statement. How can I change my query to get this to work as expected.

          where and a1.TS between (('01-MAR-18 06:40:00.0' and '02-MAR-18 06:40:00.0') or ('04-MAR-18 06:40:00.0' and '05-MAR-18 06:40:00.0'))
                and a1.name =  'WV3_NDC_Avg123_Moistur'                                                    
                and a2.name =  'WV3_SAWDI_NO'
                and a3.name =  'WV3_PRODUCT'
                and a4.name =  'WV3_CI_FO_NO_CHUTES'
                and a5.name =  'WV3_VS_DFWL_COL_B'
                and a6.name =  'WV3_FOM_ONSTS'
                and a8.name =  'WV3_OVM_ONSTS'
                and a9.name =  'WV3_PI_OV_RAMP_HGHT_SF'
                and a10.name = 'WV3_PULLCAM_TOT_MINMONTH'
                and a11.name = 'WV3_VS_BAT_HGT_AVG'
                and a12.name = 'WV3_LD_BOVEN_S'
                and a13.name = 'WV2_THEO_LSPD_ADJ'

This query is written in Aspen SQLPlus and is accessing a historian database. For the query to work I create a temp table, and query that table. I do not know if this makes a difference, but worth noting.

Also,

where a1.TS between '01-MAR-18 06:40:00.0' and '02-MAR-18 06:40:00.0'

works just fine.


Solution

  • You need to repeat the column name:

    where ( (a1.TS between '01-MAR-18 06:40:00.0' and '02-MAR-18 06:40:00.0') or
            (a1.TS between '04-MAR-18 06:40:00.0' and '05-MAR-18 06:40:00.0')
          ) and
          . . .