如何在SQL查询中同时选择单独的时间间隔?

时间:2018-03-07 18:43:48

标签: sql sqlplus historian aspen

以下是我的SQL查询的一部分。我想从显示的时间段中选择数据。我有许多具体的日期范围,我需要包含在'或'声明。如何更改我的查询以使其按预期工作。

          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'

此查询是在Aspen SQLPlus中编写的,并且正在访问历史数据库。为了使查询起作用,我创建了一个临时表,并查询该表。我不知道这是否有所作为,但值得注意。

此外,

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

工作正常。

1 个答案:

答案 0 :(得分:3)

您需要重复列名称:

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
      . . .