我有16个colymns表,而第一列是时间戳(即16/02/2011 00:00:00),表中填充了5分钟的测量值,这意味着每列和日期将有288个5分钟的测量值。如果我想计算,比如每天和每月大于> 100毫秒的“RTTD”列的值,解决方案是什么?
答案 0 :(得分:3)
我创建了一个简单的表(我没有打扰所有16列):
SQL> desc t23
Name Null? Type
----------------------- -------- -----------------
TS TIMESTAMP(6)
RTTD NUMBER
STN_ID NUMBER
SQL>
这个简短的匿名区块为四个站点生成了四天的读数:
declare
dt timestamp := trunc(systimestamp, 'MM');
begin
for r in 1..1152 loop
insert into t23 values (dt, round(dbms_random.value(0,120)), 11);
insert into t23 values (dt, round(dbms_random.value(0,120)), 22);
insert into t23 values (dt, round(dbms_random.value(0,99)), 33);
if r < 600 then
insert into t23 values (dt, round(dbms_random.value(0,120)), 44);
else
insert into t23 values (dt, round(dbms_random.value(0,80)), 44);
end if;
dt := dt + interval '5' minute;
end loop;
end;
/
因此,此查询将汇总四天内四个电台的读数。 COUNT()
忽略NULL值,SELECT利用这一点:CASE()
对任何小于100的RTTD值返回null。
SQL> select trunc(ts) as dt
2 , stn_id
3 , count(*) as tot_reads
4 , count(case when rttd >= 100 then rttd else null end) as rttd_100
5 from t23
6 group by trunc(ts), stn_id
7 order by 1, 2
8 /
DT STN_ID TOT_READS RTTD_100
--------- ---------- ---------- ----------
01-FEB-11 11 288 35
01-FEB-11 22 288 51
01-FEB-11 33 288 0
01-FEB-11 44 288 54
02-FEB-11 11 288 52
02-FEB-11 22 288 48
02-FEB-11 33 288 0
02-FEB-11 44 288 53
03-FEB-11 11 288 51
03-FEB-11 22 288 43
03-FEB-11 33 288 0
03-FEB-11 44 288 2
04-FEB-11 11 288 48
04-FEB-11 22 288 45
04-FEB-11 33 288 0
04-FEB-11 44 288 0
16 rows selected.
SQL>
要按月汇总,您只需将TRUNC(ts)
替换为TRUNC(ts, 'MM')
,其效果如下:
SQL> select systimestamp as now
2 , trunc(systimestamp) as today
3 , trunc(systimestamp, 'MM') as fom
4 from dual
5 /
NOW TODAY FOM
------------------------------------ --------- ---------
23-FEB-11 11.39.29.127000 +00:00 23-FEB-11 01-FEB-11
SQL>
答案 1 :(得分:1)
不确定你想要什么。问题定义过于模糊,我不确定你所谈论的那张桌子究竟是什么......
无论如何,我想它会是这样的:
SELECT
EXTRACT(YEAR FROM FIRST_COLUMN) AS YEAR_,
EXTRACT(MONTH FROM FIRST_COLUMN) AS MONTH_,
EXTRACT(DAY FROM FIRST_COLUMN) AS DAY_,
COUNT(*) AS TOTAL
FROM
SOME_TABLE
WHERE
RTTD > 100
GROUP BY
EXTRACT(YEAR FROM FIRST_COLUMN),
EXTRACT(MONTH FROM FIRST_COLUMN),
EXTRACT(DAY FROM FIRST_COLUMN),