计算两个日期之间的第90个百分点

时间:2016-08-06 17:51:52

标签: sql postgresql

嗨我有一个有4列的表,其中2列是日期列,我需要找到它们之间的差异,然后找到第90个百分位

表A

ID,Name,Start_Date,End_Date
1,abc,04/15/2014,04/16/2014 
2,xyz,05/13/2014,05/13/2014 
3,afd,05/13/2014,05/14/2014 
4,rfd,05/15/2014,05/16/2014 
5,grr,06/15/2014,06/16/2014 

非常感谢任何帮助或指示

2 个答案:

答案 0 :(得分:2)

您可以使用percentile_disc() or percentile_cont()

select percentile_cont(0.90) within group (order by date_end - date_start)
from t;

答案 1 :(得分:1)

Declare @Table table (ID int, Name varchar(50),Start_Date Date,End_Date Date)
Insert into @Table values
(1,'abc','04/15/2014','04/16/2014'),
(2,'xyz','05/13/2014','05/13/2014'),
(3,'afd','05/13/2014','05/14/2014'), 
(4,'rfd','05/15/2014','05/16/2014'), 
(5,'grr','06/15/2014','06/16/2014') 

Select DateR1=min(Start_Date),DateR2=DateAdd(DD,DateDiff(DD,min(Start_Date),max(End_Date))*.9,min(Start_Date))
 From  @Table

返回

DateR1      DateR2
2014-04-15  2014-06-09

所以要返回记录

Select A.* 
 From  @Table A
 Join (Select DateR1=min(Start_Date),DateR2=DateAdd(DD,DateDiff(DD,min(Start_Date),max(End_Date))*.9,min(Start_Date)) From  @Table) B
   on (A.Start_Date Between B.DateR1 and B.DateR2)

返回5条原始记录中的4条

ID  Name    Start_Date  End_Date
1   abc     2014-04-15  2014-04-16
2   xyz     2014-05-13  2014-05-13
3   afd     2014-05-13  2014-05-14
4   rfd     2014-05-15  2014-05-16