在sql oracle中的两个日期之间选择每小时计数?

时间:2014-03-17 14:06:02

标签: sql oracle

我无法在论坛中找到我如何从两个日期之间的sql计数中选择,但每小时一次。我必须从服务器中提取三个不同的列,包括日期,小时和计数。最后,数据库将如下所示:

    date     hour  count
  29/08/2010,"06","84"
  29/08/2010,"07","95"
  29/08/2010,"08","125"
  29/08/2010,"09","133"
  29/08/2010,"10","99"
  29/08/2010,"11","105"
  29/08/2010,"12","109"
  29/08/2010,"13","135"
  29/08/2010,"14","160"
  29/08/2010,"15","119"
  29/08/2010,"16","127"
  29/08/2010,"17","126"
  29/08/2010,"18","147"
  29/08/2010,"19","125"
  29/08/2010,"20","135"
  29/08/2010,"21","108"
  29/08/2010,"22","12"
  30/08/2010,"04","3"
  30/08/2010,"05","92"
  30/08/2010,"06","438"
  30/08/2010,"07","471"
  30/08/2010,"08","329"
  30/08/2010,"09","212"
    .  ..  . ..  . .
   ..  . . . .  . . . 

如果我们在谈论日期,变量名称是TRXDATE。我知道是这样的:

          TO_DATE(TO_CHAR(TRXDATE,'DD/MM,"TO_CHAR(TRXDATE,'HH24')","COUNT(*)"

在两个日期之间完全是这样的:

  select * from sgco.cardusage where trxdate between TO_DATE('20/01/2014','dd/mm/yyyy') and TO_DATE('21/01/2014','dd/mm/yyyy')

但是我如何能够加入这两个命令,两个日期之间每小时计算两次?

非常感谢。

2 个答案:

答案 0 :(得分:0)

您应该通过group by

获取
select TO_CHAR(trunc(trxdate), 'dd/mm/yyyy') day,
       TO_CHAR(trunc(trxdate, 'hh'), 'hh24') hour,
       count(*) nb
from sgco.cardusage
where trxdate between TO_DATE('20/01/2014','dd/mm/yyyy')
                  and TO_DATE('22/01/2014','dd/mm/yyyy')
group by trunc(trxdate), trunc(trxdate, 'hh')

答案 1 :(得分:0)

如下所示?

SQL> with t (date#, value#) as (
  2   select sysdate+rownum/(24*5), mod(abs(round(dbms_random.value(1,100),0)),9)
  3   from dual connect by level <= 100
  4  )
  5  select trunc(trunc(date#,'HH24'),'DD') trxdate, to_char(trunc(date#,'HH24'),'HH24') hour#, count(*) trxdate from t
  6  group by trunc(date#,'HH24')
  7  order by 1,2
  8  /

TRXDATE  HO    TRXDATE                                                          
-------- -- ----------                                                          
17.03.14 18          3                                                          
17.03.14 19          5                                                          
17.03.14 20          5                                                          
17.03.14 21          5                                                          
17.03.14 22          5                                                          
17.03.14 23          5                                                          
18.03.14 00          5                                                          
18.03.14 01          5                                                          
18.03.14 02          5                                                          
18.03.14 03          5                                                          
18.03.14 04          5                                                          
18.03.14 05          5                                                          
18.03.14 06          5                                                          
18.03.14 07          5                                                          
18.03.14 08          5                                                          
18.03.14 09          5                                                          
18.03.14 10          5                                                          
18.03.14 11          5                                                          
18.03.14 12          5                                                          
18.03.14 13          5                                                          
18.03.14 14          2