gpt4 book ai didi

PostgreSQL 按季节分组(北半球和南半球)

转载 作者:行者123 更新时间:2023-11-29 12:19:51 25 4
gpt4 key购买 nike

我的数据点分布在全局范围内。每个数据点都有一个时间戳。

我想按季节对数据进行分组,如图所示(来源:https://en.wikipedia.org/wiki/Season#/media/File:Seasons1.svg)。

enter image description here

这是我到目前为止尝试过的(北半球 2011/2012 冬季):

SELECT * FROM my_table 
WHERE my_date BETWEEN '2011-12-21' AND '2012-03-21'
AND ST_Y(ST_Centroid(geom)) > 0;

我怎样才能在所有可能的年份都这样做?我试过 ...OR date BETWEEN... 但这很慢。

怎么可能同时选择北半球和南半球的冬天?

更新

对于北半球的所有冬季数据,我使用以下查询:

CREATE TABLE season_winter_north AS WITH first_step AS (
SELECT
id,
extract(month from my_date)::int AS month,
extract(day from my_date)::int AS day,
ST_Centroid(geom) AS geom
FROM mytable
WHERE ST_Y(ST_Centroid(geom)) > 0)

(SELECT * FROM first_step
WHERE month = 12
AND day >= 21)

UNION ALL

(SELECT * FROM first_step
WHERE month = ANY('{1,2}'))

UNION ALL

(SELECT * FROM first_step
WHERE month = 3
AND day<21)

是否有更优雅或更高效的解决方案?

最佳答案

with t(my_date) as (
values ('2012-07-01'::date), ('2013-03-30'), ('2013-10-12'), ('2013-01-10')
)
select
case
when my_date between
make_date(extract(year from my_date)::int, 3, 21)
and
make_date(extract(year from my_date)::int, 6, 20)
then 'spring'
when my_date between
make_date(extract(year from my_date)::int, 6, 21)
and
make_date(extract(year from my_date)::int, 9, 22)
then 'summer'
when my_date between
make_date(extract(year from my_date)::int, 9, 23)
and
make_date(extract(year from my_date)::int, 12, 20)
then 'fall'
else 'winter'
end as season,
my_date
from t
;
season | my_date
--------+------------
summer | 2012-07-01
spring | 2013-03-30
fall | 2013-10-12
winter | 2013-01-10

关于PostgreSQL 按季节分组(北半球和南半球),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32393295/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com