记录低于2列之间的平均天数

时间:2014-11-21 13:11:57

标签: oracle date average

我正在试图弄清楚如何只选择start_date和end_date之间天数少于平均值的记录。

我的桌子上有3条记录,我用avg尝试了很多方法,但我似乎没有完成它。这就是我创建的表(大学)的样子:

  

dep_id / class_id / start_date / end_date / comments

     

7500/10050 / 10-FEB-10/20-FEB-10 / clean

     

7501/10060 / 17-MAR-10/22-MAR-10 / dirty

     

7502/10070 / 21-JUN-10/29-JUL-10 / clean

这就是我迄今为止所尝试过的内容:

select dep_id, class_id, start_date, end_date, comments
from university
where dep_id = (select avg( cast(end_date as DATE) - cast(start_date as date)) as avg_days
from university);

1 个答案:

答案 0 :(得分:0)

试试这个:

SELECT dep_id,class_id,start_date,end_date,comments
  FROM (
  SELECT t1.*,AVG(TO_DATE(end_date,'dd-mon-yy') - TO_DATE(start_date,'dd-mon-yy')) OVER() total_avg
  ,TO_DATE(end_date,'dd-mon-yy') - TO_DATE(start_date,'dd-mon-yy') diff  FROM university t1) q
  WHERE diff< total_avg