你有一个糟糕、糟糕、糟糕的数据格式。这很明显,因为您的数据分布在列周围。数据应如下所示:
month, year, lat, lng, y
所以,一行应该是 10。你可以在 Postgres 中这样做:
select t.month, u.ord + 2000 as year, t.lat t.lng, u.y
from t, lateral
unnest(array[y1, y2, . . .]) with ordinality u(y, ord)
接下来,您可以将年/月转换为日期:
select to_date(u.ord + 2000 || '-' || t.month || '-01', 'YYYY-MM-DD') as yyyymmdd, t.lat t.lng, u.y
from t, lateral
unnest(array[y1, y2, . . .]) with ordinality u(y, ord);
现在您可以将其用作子查询并使用between:
select t.*
from (select to_date(u.ord + 2000 || '-' || t.month || '-01', 'YYYY-MM-DD') as yyyymmdd, t.lat t.lng, u.y
from t, lateral
unnest(array[y1, y2, . . .]) with ordinality u(y, ord)
) t
where yyyymmdd between '2003-04-01'::timestamp and '2005-06-01'::timestamp