可能有数百种方法可以实现它。您可以首先在一个CTE
或子查询中创建您需要的季度,以便您可以将其与您的定价表进行比较,例如
样本数据
CREATE TEMPORARY TABLE pricing (id INT, start_date DATE,price NUMERIC);
INSERT INTO pricing VALUES
(5,CURRENT_DATE,1.99),
(4,CURRENT_DATE-2,0.99),
(3,CURRENT_DATE-3,0.42),
(2,CURRENT_DATE-100,0.10),
(1,CURRENT_DATE-121,0.82);
询问
WITH q (year,quarter) AS (
SELECT DISTINCT
EXTRACT(YEAR FROM start_date::TIMESTAMP),
EXTRACT(QUARTER FROM start_date::TIMESTAMP)
FROM pricing
)
SELECT *, (SELECT price
FROM pricing
WHERE EXTRACT(QUARTER FROM start_date::TIMESTAMP) = q.quarter AND
EXTRACT(YEAR FROM start_date::TIMESTAMP) = q.year
ORDER BY start_date LIMIT 1)
FROM q ORDER BY q.year,q.quarter;
year | quarter | price
------+---------+-------
2020 | 4 | 0.82
2021 | 1 | 0.42
演示:db<>fiddle
编辑:根据您的用例,您可能希望使用类似的东西生成年份和季度,generate_series
而不是如上例所示运行完整扫描pricing
,例如
SELECT
EXTRACT(YEAR FROM d) AS year,
EXTRACT(QUARTER FROM d) AS quarter
FROM generate_series('2019-01-01'::DATE,CURRENT_DATE,'3 month') AS d
ORDER BY 1,2;
year | quarter
------+---------
2019 | 1
2019 | 2
2019 | 3
2019 | 4
2020 | 1
2020 | 2
2020 | 3
2020 | 4
2021 | 1