如何一次运行具有不同值的单个查询?

时间:2018-10-21 05:55:01

标签: sql oracle toad

我在下面的查询中需要每次更新日期和时间。谁能帮助我找出运行单个查询并提取多个输入结果的方式?

select channel
       ,TO_CHAR(ECH.CH_VALIDFROM,'dd/mm/yyyy')
       , count(1) count 
from emcdu.ext_contract_history ech 
where ch_status='a' 
and CH_VALIDFROM BETWEEN TO_DATE('17/10/2018 20:21:00','dd/mm/yyyy hh24:mi:ss') 
                 AND TO_DATE ('17/10/2018 21:16:00','dd/mm/yyyy hh24:mi:ss') 
and ch_validfrom=(select min(CH_VALIDFROM) 
                  from emcdu.ext_contract_history 
                  where co_id=ech.co_id 
                  and ch_status='a') 
group by channel
       , TO_CHAR(ECH.CH_VALIDFROM,'dd/mm/yyyy');

我具有以下日期和时间值:

('16/10/2018 11:41:00','dd/mm/yyyy hh24:mi:ss') AND TO_DATE
('16/10/2018 12:01:00','dd/mm/yyyy hh24:mi:ss')

('16/10/2018 12:38:00','dd/mm/yyyy hh24:mi:ss')  AND TO_DATE 
('16/10/2018 13:13:00','dd/mm/yyyy hh24:mi:ss') 

('16/10/2018 20:21:00','dd/mm/yyyy hh24:mi:ss')   AND TO_DATE
('16/10/2018 20:30:00','dd/mm/yyyy hh24:mi:ss')

2 个答案:

答案 0 :(得分:1)

根据我对您的评论的理解,您可能希望为每个范围定义一个范围ID,并将其包含在group by中,然后将这些范围放入CTE(或范围表)中

with inputs
AS
(
SELECT 1 in_range_id,
       TO_DATE('16/10/2018 11:41:00','dd/mm/yyyy hh24:mi:ss') as start_dtime,
       TO_DATE('16/10/2018 12:01:00','dd/mm/yyyy hh24:mi:ss') as end_dtime

FROM dual
UNION ALL
SELECT 2,
       TO_DATE('16/10/2018 12:38:00','dd/mm/yyyy hh24:mi:ss'),
       TO_DATE('16/10/2018 13:13:00','dd/mm/yyyy hh24:mi:ss')
FROM dual
UNION ALL
SELECT 3,
       TO_DATE('16/10/2018 20:21:00','dd/mm/yyyy hh24:mi:ss'),
       TO_DATE('16/10/2018 20:30:00','dd/mm/yyyy hh24:mi:ss')
FROM dual
)
select in_range_id
       ,channel
       ,TO_CHAR(ECH.CH_VALIDFROM,'dd/mm/yyyy')
       , count(1) count 
from emcdu.ext_contract_history ech cross join inputs i
where ch_status='a' 
and ch_validfrom BETWEEN  i.start_dtime 
                     AND  i.end_dtime
and ch_validfrom=(select min(CH_VALIDFROM) 
                  from emcdu.ext_contract_history 
                  where co_id=ech.co_id 
                  and ch_status='a') 
group by in_range_id
         ,channel
         ,TO_CHAR(ECH.CH_VALIDFROM,'dd/mm/yyyy');

答案 1 :(得分:1)

ORing 很少(即使用WHERE运算符将这些条件放入相同的OR子句中):

select   channel
       , to_char(ech.ch_validfrom, 'dd/mm/yyyy')
       , count(*) count 
from emcdu.ext_contract_history ech 
where ch_status = 'a' 
and (   ch_validfrom between to_date('17/10/2018 20:21:00','dd/mm/yyyy hh24:mi:ss') 
                         and to_date('17/10/2018 21:16:00','dd/mm/yyyy hh24:mi:ss') 
     or ch_validfrom between to_date('16/10/2018 11:41:00','dd/mm/yyyy hh24:mi:ss') 
                         and to_date('16/10/2018 12:01:00','dd/mm/yyyy hh24:mi:ss')
     or ch_validfrom between to_date('16/10/2018 12:38:00','dd/mm/yyyy hh24:mi:ss')  
                         and to_date('16/10/2018 13:13:00','dd/mm/yyyy hh24:mi:ss') 
     or ch_validfrom between to_date('16/10/2018 20:21:00','dd/mm/yyyy hh24:mi:ss')   
                         and to_date('16/10/2018 20:30:00','dd/mm/yyyy hh24:mi:ss')
    )
and ch_validfrom=(select min(ch_validfrom) 
                  from emcdu.ext_contract_history 
                  where co_id = ech.co_id 
                  and ch_status = 'a') 
group by channel
       , to_char(ech.ch_validfrom, 'dd/mm/yyyy');