oracle sql count(*),行不匹配

时间:2016-07-29 22:15:09

标签: sql database oracle datetime outer-join

我有一个oracle查询:

select to_char(te.HORA, 'hh24:mi') HORARIO, count(1) CANTIDAD 
from db.t_error te
where (te.error LIKE 'ERR-108' or te.error LIKE 'ERR-256')
and te.HORA >= to_HORA('29-07-2016 18:50', 'dd-mm-yyyy hh24:mi')
and te.HORA <= to_HORA('29-07-2016 19:00', 'dd-mm-yyyy hh24:mi')
group by to_char(te.HORA, 'hh24:mi')
order by to_char(te.HORA, 'hh24:mi');

结果(表):

HORARIO | CANTIDAD
18:53            2
18:56            2
18:58            1
18:59            1

但我需要结果包括所有分钟(表格):

HORARIO | CANTIDAD
18:50            0
18:51            0
18:52            0
18:53            2
18:54            0
18:55            0
18:56            2
18:57            0
18:58            1
18:59            1
19:00            0

使用0值计算分钟到分钟或计数(1)的结果不匹配。

我希望有所帮助!

感谢。

2 个答案:

答案 0 :(得分:1)

这不是一个好的解决方案(应该实现它直接与您的代码一起工作,而不是作为附加组件),但它说明了应该如何完成。

在此上下文中,

to_date将时间(使用hh24:mi格式模型)添加到当月的第一天,但​​由于您提取小时和分钟,因此您不关心无论如何都要放弃其余的。

在代码中修复的最大问题是您使用字符串。最好将所有内容放入日期数据类型,按日期数据类型(到分钟),并且仅在最后使用to_char()用于显示目的。如果您需要帮助,请回信。

with
     your_table ( horario, cantidad ) as (
       select '18:53', 2 from dual union all
       select '18:56', 2 from dual union all
       select '18:58', 1 from dual union all
       select '18:59', 1 from dual
     ),
     all_times ( horario ) as (
       select to_char( to_date('18:50', 'hh24:mi') + (level - 1) / (24 * 60), 'hh24:mi')
       from   dual
       connect by level <= 1 + (to_date('19:00', 'hh24:mi') - 
                                                 to_date('18:50', 'hh24:mi')) * 24 * 60
     )
select a.horario, nvl(y.cantidad, 0) as cantidad
from   all_times a left outer join your_table y
                   on a.horario = y.horario
order by horario
;

HORARIO   CANTIDAD
------- ----------
18:50            0
18:51            0
18:52            0
18:53            2
18:54            0
18:55            0
18:56            2
18:57            0
18:58            1
18:59            1
19:00            0

11 rows selected.

<强>被修改

对于没有额外工作的“廉价”解决方案,您可以将原始查询直接插入“your_table”因子子查询中,就像这样(但我没有基表,所以我无法测试)。

with
     your_table ( horario, cantidad ) as (
       select   to_char(te.HORA, 'hh24:mi') HORARIO, count(1) CANTIDAD 
       from     db.t_error te
       where    (te.error LIKE 'ERR-108' or te.error LIKE 'ERR-256')
         and    te.HORA >= to_HORA('29-07-2016 18:50', 'dd-mm-yyyy hh24:mi')
         and    te.HORA <= to_HORA('29-07-2016 19:00', 'dd-mm-yyyy hh24:mi')
       group by to_char(te.HORA, 'hh24:mi')        
     ),
     all_times ( horario ) as (
       select to_char( to_date('18:50', 'hh24:mi') + (level - 1) / (24 * 60), 'hh24:mi')
       from   dual
       connect by level <= 1 + (to_date('19:00', 'hh24:mi') - 
                                                 to_date('18:50', 'hh24:mi')) * 24 * 60
     )
select a.horario, nvl(y.cantidad, 0) as cantidad
from   all_times a left outer join your_table y
                   on a.horario = y.horario
order by horario
;

答案 1 :(得分:-1)

您无法选择不存在的数据

您正在寻找概念的零线反映数据库中不存在的条目。您需要在应用程序中构建此类逻辑。