· 6 years ago · Mar 14, 2019, 11:28 AM
1create table if not exists somedates (
2startdate date,
3otherdates date[]);
4
5insert into somedates (startdate, otherdates) values
6('2000-11-14', '{2001-10-30}'),
7('2001-09-26', '{2002-09-20,2004-09-21,2006-09-19,2010-09-17}'),
8('2005-05-10', '{2006-08-03,2008-08-12}'),
9('2004-09-29', '{2005-09-29,2006-10-02,2007-10-04,2009-09-22,2010-08-06,2011-09-01,2013-09-17}');
10
11SELECT *
12FROM somedates
13WHERE any(otherdates) - startdate > 0.6*365
14 and any(otherdates) - startdate < 1.4*365
15 and any(otherdates) - startdate > 3.5*365
16 and any(otherdates) - startdate < 5.5*365
17;
18
19select * from
20 (SELECT *, generate_subscripts(otherdates, 1) as s
21 FROM somedates) as foo
22where (otherdates[s] - startdate) >= 0.6*365
23 and (otherdates[s] - startdate) <= 1.4*365
24 and (
25 ((otherdates[s+1] - startdate) >= 3.5*365
26 and (otherdates[s+1] - startdate) <= 5.5*365)
27 or ((otherdates[s+2] - startdate) >= 3.5*365
28 and (otherdates[s+2] - startdate) <= 5.5*365)
29 or ((otherdates[s+3] - startdate) >= 3.5*365
30 and (otherdates[s+3] - startdate) <= 5.5*365))
31;