日期:2014-05-17  浏览次数:20742 次

问一个分析函数的问题,如果在分析函数中加入条件?
下面是从一个表中,取当天向前推移99个样本(+当天的话为100个样本了)的每天的标准差。

select id,stdev(CountNum) over (PARTITION BY id order by s_date asc rows between 99 preceding and 0 following)
from table
order by id,s_date;

现在我要这样一个效果:在取每天的标准差时,过滤不合理的数据(假设CountNum>10的为不合理),请问怎样实现这种效果?

附:因为过滤了不合理的数据,所以样本天数是会变化的,不再=100。所以,这个CountNum>10的条件,是不能直接加到where 条件里面去的。

下面这个是错误的:
select id,stdev(CountNum) over (PARTITION BY id order by s_date asc rows between 99 preceding and 0 following)
from table
where CountNum>10
order by id,s_date;

这个写法是正确的(假设sql语法正确的话)
select id,stdev(CountNum) over (PARTITION BY id order by s_date asc rows between 99 preceding and 0 following where CountNum>10)
from table
order by id,s_date;


------解决方案--------------------
可以试下这个:

select id stdev(countnum) over (partition by id order by s_date asc rows between 99 precedint and 0 following)

from 

( select * from table where countnum<=10) newtable

order by id,s_date

------解决方案--------------------
SQL code
http://www.itpub.net/viewthread.php?tid=608107&extra=&page=1
oracle分析函数


oracle分析函数


zhouwf0726 | 25 七月, 2006 12:51

oracle分析函数--SQL*PLUS环境
--1、GROUP BY子句 

--CREATE TEST TABLE AND INSERT TEST DATA.
create table students
(id number(15,0),
area varchar2(10),
stu_type varchar2(2),
score number(20,2));

insert into students values(1, '111', 'g', 80 );
insert into students values(1, '111', 'j', 80 );
insert into students values(1, '222', 'g', 89 );
insert into students values(1, '222', 'g', 68 );
insert into students values(2, '111', 'g', 80 );
insert into students values(2, '111', 'j', 70 );
insert into students values(2, '222', 'g', 60 );
insert into students values(2, '222', 'j', 65 );
insert into students values(3, '111', 'g', 75 );
insert into students values(3, '111', 'j', 58 );
insert into students values(3, '222', 'g', 58 );
insert into students values(3, '222', 'j', 90 );
insert into students values(4, '111', 'g', 89 );
insert into students values(4, '111', 'j', 90 );
insert into students values(4, '222', 'g', 90 );
insert into students values(4, '222', 'j', 89 );
commit;

col score format 999999999999.99

--A、GROUPING SETS

select id,area,stu_type,sum(score) score 
from students
group by grouping sets((id,area,stu_type),(id,area),id)
order by id,area,stu_type;

/*--------理解grouping sets
select a, b, c, sum( d ) from t
group by grouping sets ( a, b, c )

等效于

select * from (
select a, null, null, sum( d ) from t group by a
union all
select null, b, null, sum( d ) from t group by b 
union all
select null, null, c, sum( d ) from t group by c 
)
*/

--B、ROLLUP

select id,area,stu_type,sum(score) score 
from students
group by rollup(id,area,stu_type)
order by id,area,stu_type;

/*--------理解rollup
select a, b, c, sum( d )
from t
group by rollup(a, b, c);

等效于

select * from (
select a, b, c, sum( d ) from t group by a, b, c 
union all
select a, b, null, sum( d ) from t group by a, b
union all
select a, null, null, sum( d ) from t group by a
union all
select null, null, null, sum( d ) from t
)
*/

--C、CUBE

select id,area,stu_type,sum(score) score 
from students
group by cube(id,area,stu_type)
order by id,area,stu_type;

/*--------理解cube
select a, b, c, sum( d ) from t
group by cube( a, b, c)

等效于

select a, b, c, sum( d ) from t
group by grouping sets( 
( a, b, c ), 
( a, b ), ( a ), ( b, c ), 
( b ), ( a, c ), ( c ), 
() )
*/

--D、GROUPING

/*从上面的结果中我们很容易发现,每个统计数据所对应的行都会出现null,
如何来区分到底是根据那个字段做的汇总呢,grouping函数判断是否合计列!*/

select decode(grouping(id),1,'all id',id) id,
decode(grouping(area),1,'all area',to_char(area)) area,
decode(grouping(stu_type),1,'all_stu_type',stu_type) stu_type,
sum(score) score
from students
group by cube(id,area,stu_type)
order by id,area,stu_type; 

--2、OVER()函数的使用