按天分组

时间:2018-11-12 18:44:59

标签: oracle

我有一个表MSGCOUNT,如下所示:

SERVER  CHANNEL  VALUE  VARIABLE      VARNAME      DATES      TIME
---------------------------------------------------------------------
1       A        133    MessageCount  MSG_COUNT_A 12-NOV-18  10:16:56
1       A        129    MessageCount  MSG_COUNT_A 12-NOV-18  10:13:26
1       A        120    MessageCount  MSG_COUNT_A 12-NOV-18  10:00:55 
2       B        210    MessageCount  MSG_COUNT_B 12-NOV-18  21:42:45 
2       B        200    MessageCount  MSG_COUNT_B 12-NOV-18  21:41:25
2       B        195    MessageCount  MSG_COUNT_B 12-NOV-18  21:41:19

我希望能够统计当天收到的总邮件数。

查询结果如下:

SERVER  CHANNEL  VALUE  VARIABLE      VARNAME      DATE      
----------------------------------------------------------
1       A        13    MessageCount  MSG_COUNT_A 12-NOV-18
2       B        15    MessageCount  MSG_COUNT_B 12-NOV-18

我尝试使用各种功能,无论是解析功能还是其他功能,但以下是最新尝试:

select server, channel, value, variable, varname, date
from 
    (
      select time_interval as start_interval,
             time_interval as end_interval,
             round(MSG, 2) total_msg_recv,
             channel,
             server
      from
          (
            select substr(DATES, 1,9),
                   round(VALUE,2) MSG,
                   channel,
                   server
            from MSGCOUNT 
          )
    )
group by date, server, channel
order by date

这将产生以下结果(这是错误的):

SERVER CHANNEL VALUE VARIABLE     VARNAME       DATE
-----------------------------------------------------
1      A       133   MessageCount  MSG_COUNT_A  12-NOV-18
2      B       210   MessageCount  MSG_COUNT_B  12-NOV-18

谢谢

1 个答案:

答案 0 :(得分:1)

做一个简单的减法会做吗?

SQL> with msgcount(server, channel, value, dates) as
  2    (select 1, 'a', 133, date '2018-11-12' from dual union all
  3     select 1, 'a', 129, date '2018-11-12' from dual union all
  4     select 1, 'a', 120, date '2018-11-12' from dual union all
  5     --
  6     select 2, 'b', 210, date '2018-11-12' from dual union all
  7     select 2, 'b', 200, date '2018-11-12' from dual union all
  8     select 2, 'b', 195, date '2018-11-12' from dual
  9    )
 10  select server, channel, max(value) - min(value) diff, dates
 11  from msgcount
 12  group by server, channel, dates
 13  order by server;

    SERVER C       DIFF DATES
---------- - ---------- ----------
         1 a         13 12.11.2018
         2 b         15 12.11.2018

SQL>

尚不清楚DATESTIME是什么(哪种数据类型);我认为,它应该是DATE数据类型的单个列。如果是这样,您应该TRUNC对其进行设置(例如TRUNC(DATES))并删除时间部分,因为在这里这并不重要。

如果您提供更多信息,则该查询可能会更改,但总体思路还是可以的。