我有一张带有以下数据的表格
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
我需要根据“Rounded_Time”汇总数据,我必须得到“值”的SUM和“AvgTm”的AVG,然后泵入另一个表T2。该脚本将按小时计划。所以,下次,它不能选择更早的数据。
输出T2表有
Domain Mode Channel KPI Value AvgTm Rounded_Time USD Manual P1 consolidateUSD 60 3 11/14/2015 13:00 DKT Auto P2 consolidateDKT 5 4 11/14/2015 13:00
我无法正确获取输出。我试过以下。我是SQL的新手。请帮忙。
select *
from T1
where Rounded_Time interval 60
and SUM(Value)
and AVG(AvgTm)
group by Domain, Mode, Channel, KPI
答案 0 :(得分:0)
尝试这个
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子句建议什么,因为我想在数据中有一个日期/时间引用。不只是一些名为rounded_time的字符串。
答案 1 :(得分:0)
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数据库..
答案 2 :(得分:0)
正如你对我们所有的假设所说的“正确”,......
这会导致像这样的WHERE子句:
where extract(hour from systimestamp) - 1 =
extract(hour from cast(rounded_time as timestamp) - interval '1' minute)
这里我们提取批处理执行查询时的当前小时并减去一小时。这将为我们提供12个批次在13:00之后和14:00之前运行。
我们采用round_time,从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;