Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 汇总每小时数据_Sql_Oracle - Fatal编程技术网

Sql 汇总每小时数据

Sql 汇总每小时数据,sql,oracle,Sql,Oracle,我有一个带有以下数据的表T1 Domain Mode Channel KPI Value AvgTm Rounded_Time USD Manual P1 consolidateUSD 20 2 11/14/2015 12:15:00 USD Manual P1 consolidateUSD 10 4 11/14/2015 12:30:00 USD Manual P

我有一个带有以下数据的表T1

Domain Mode Channel KPI Value AvgTm Rounded_Time USD Manual P1 consolidateUSD 20 2 11/14/2015 12:15:00 USD Manual P1 consolidateUSD 10 4 11/14/2015 12:30:00 USD Manual P1 consolidateUSD 10 2 11/14/2015 12:45:00 USD Manual P1 consolidateUSD 20 4 11/14/2015 13:00:00 DKT Auto P2 consolidateDKT 5 4 11/14/2015 12:15:00 DKT Auto P2 consolidateDKT 5 4 11/14/2015 12:30:00 DKT Auto P2 consolidateDKT 5 4 11/14/2015 12:45:00 DKT Auto P2 consolidateDKT 5 4 11/14/2015 13:00:00 也许试试这个吧

SELECT
      "DOMAIN"
    , "MODE"
    , "CHANNEL"
    , "KPI"
    , TRUNC(ROUNDED_TIME,'HH24') rounded_date
    , to_number(to_char(ROUNDED_TIME,'HH24')) hr_int
    , SUM(Value)
    , AVG(AvgTm)
FROM T1
WHERE 1 = 1
GROUP BY
      "DOMAIN"
    , "MODE"
    , "CHANNEL"
    , "KPI"
    , TRUNC(ROUNDED_TIME,'HH24')
    , to_number(to_char(ROUNDED_TIME,'HH24'))
在这个阶段,我不知道对WHERE子句有什么建议,因为我希望数据中有一个日期/时间引用。不仅仅是一个叫做四舍五入时间的字符串

select distinct 
t.channel,  dDomain,mMode,KPI,sum(value1) as sumValue ,sum(AvgTm )  /count(AvgTm) as AvgTm,
x.Rounded_Time
    from 
    (select * from 
    test2
    where 
     Rounded_Time  = current_timestamp - interval  '1' hour )   t
    inner join
    (
select Rounded_Time  ,channel
from test2 
where 
     Rounded_Time  = current_timestamp - interval  '1' hour 
qualify row_number() over(partition by channel order by Rounded_Time desc) =1
) X
    on t.channel = x.channel
    group  by 
t.channel,  dDomain,mMode,KPI,x.Rounded_Time
它正在使用Teradata数据库。

正如您所说的“正确”我们所有的假设

  • 您只使用时间的时间部分,我们必须忽略日期部分
  • 选择12:15、12:30、12:45和13:00的批次在13:00之后和14:00之前运行
这将导致类似以下内容的WHERE子句:

where extract(hour from systimestamp) - 1 = 
      extract(hour from cast(rounded_time as timestamp) - interval '1' minute)
这里,我们提取批次执行查询时的当前小时数,并减去一小时。对于13:00之后和14:00之前运行的批次,这将为我们提供12

我们取四舍五入的时间,减去一分钟,从12:00到11:59,从13:00到12:59,然后提取小时。所以12:00到13:00之间的所有记录都有12个

完整查询:

select 
  domain, mode, channel, kpi,
  sum(value),
  avg(avgtm),
  to_char(sysdate, 'hh24') || ':00'
from t1
where extract(hour from systimestamp) - 1 = 
      extract(hour from cast(rounded_time as timestamp) - interval '1' minute)
group by domain, mode, channel, kpi;

这些列的数据类型是什么?特别是“四舍五入时间”-它是字符串吗?。列中根本没有日期吗?(坏主意)顺便问一下,你确定取4个数据点的平均值是总结“平均值”的准确方法吗?这次应该选择12:15、12:30、12:45和13:00,下次是13:15、13:30、13:45和14:00?那么这次批处理在13:00之后和14:00之前运行,下次批处理在14:00之后和15:00之前运行?明天今天的记录(例如12:15)将被新记录取代?正确。“Rounded_Time”的数据类型是“Date”。@tester:值得仔细考虑一下已经被_评论使用的_;次Average的Average不一定是总Average。AVG(AVG(1,1,1),AVG(5))=AVG(1,5)=3,但AVG(1,1,1,5)=2。“舍入时间”的数据类型是“日期”。好的,然后我们可以使用TRUNC()。将为此修改查询。但是你对“一小时”的处理是不标准的,通常12:00:00到12:59:59.999是12点而不是13点(13:00:00开始)。对我来说太晚了,我已经做了我能做的,希望它不起作用。它给出了错误:ORA-30076:extract Source的extract字段无效抱歉,我不知道Oracle执行extract的方式如此糟糕,以至于它无法从日期类型中提取时间部分(尽管日期是Oracle的DATETIME)。必须强制转换时间戳才能使其工作。我已经更新了我的答案。它不起作用。它既不为“value”也不为“avgtm”赋予价值,我不知道为什么。也许先试试这个WHERE子句而不加聚合,看看你得到了什么记录?好的。但我也需要拿到“四舍五入时间”。但由于我们没有将其添加到“groupby”中,因此在添加它时会抛出ORA-00979:不是groupby表达式错误。如何解决这个问题?。
where extract(hour from systimestamp) - 1 = 
      extract(hour from cast(rounded_time as timestamp) - interval '1' minute)
select 
  domain, mode, channel, kpi,
  sum(value),
  avg(avgtm),
  to_char(sysdate, 'hh24') || ':00'
from t1
where extract(hour from systimestamp) - 1 = 
      extract(hour from cast(rounded_time as timestamp) - interval '1' minute)
group by domain, mode, channel, kpi;