Calculate value per day

时间:2015-08-25 20:52:17

标签: sql oracle oracle11g

I have Oracle table where I insert data about network upload and download speed.

CREATE TABLE AGENT_HISTORY(
  EVENT_DATE DATE,
  NETWORK_UP NUMBER,
  NETWORK_DOWN NUMBER
)

I want to generate Bar chart for last 30 days and display total upload traffic per day(24 hours).

select * from AGENT_HISTORY where EVENT_DATE >= SYSDATE - 30;

The problem which I don't know how to solve is how I can calculate the traffic for each day from the column NETWORK_UP. The result of the query should be 30 days with total upload traffic for each day. Is this possible without PL/SQL procedure?

2 个答案:

答案 0 :(得分:2)

您可以执行此类查询,以便每天汇总network_upnetwork_down列的总计数据。

select  trunc(event_day,'day') event_date
       ,sum(network_up) tot_network_up
       ,sum(network_down) tot_network_down
from agent_history
where event_day >= trunc(sysdate,'day') - interval '30' day
group by trunc(event_day,'day');

你可以不用在PL / SQL存储代码中嵌入查询,这取决于你用于前端的东西,在java中你可以使用这样的东西

Statement stmt = null;
String schema_name = 'abc';
String query = "select trunc(event_day,'day') event_date," +
               "sum(network_up) tot_network_up," +
               "sum(network_down) tot_network_down " +
               "from " + schema_name +".agent_history " +
               "where event_day >= trunc(sysdate,'day') " +
               "- interval '30' day " +
               "group by trunc(event_day,'day')"
try {
    stmt = con.createStatement();
    ResultSet rs = stmt.executeQuery(query);
    while (rs.next()) {
        String event_data = rs.getString("EVENT_DATE");
        int tot_network_up = rs.getInt("TOT_NETWORK_UP");
        int tot_network_down= rs.getInt("TOT_NETWORK_DOWN");
      .....
}
catch (SQLException e) {
    .......
} finally {
    ......
}

使用类似的东西你只需要执行纯SQL。

答案 1 :(得分:1)

我的猜测是你只想聚合数据。像

这样的东西
SELECT trunc(event_date), 
       sum(network_up) total_up,
       sum(network_down) total_down
  FROM agent_history
 WHERE event_date >= trunc(sysdate) - 30
 GROUP BY trunc(event_date)

如果这不是您想要的,那么发布一些样本数据,预期输出等会非常有帮助。