日期:2014-05-17 浏览次数:21089 次
select sum(DAT)/count(*) from ( select D_FLG1 as D_FLG, DAT1 as DAT from table where ... union all select D_FLG2 as D_FLG, DAT2 as DAT from table where ... union all ... )where D_FLG...
------解决方案--------------------
楼主参考一下:
SQL> select * from flag;
FLAG DATA
---------- ----------
2 100
2 100
2 100
2 100
1 100
2 105
2 105
1 100
1 100
2 100
已选择10行。
flag=2表示有效;
flag=1表示无效.
rownum是你要取的小时数,你也可以改为区间形式。
SQL> select sum(data*decode(flag,2,1,1,0,1))/sum(decode(flag,2,1,1,0,1)) result from flag where rownum<=4;
RESULT
----------
100
SQL> select sum(data*decode(flag,2,1,1,0,1))/sum(decode(flag,2,1,1,0,1)) result from flag where rownum<=7;
RESULT
----------
101.666667
SQL> select sum(data*decode(flag,2,1,1,0,1))/sum(decode(flag,2,1,1,0,1)) result from flag where rownum<=10;
RESULT
----------
101.428571