0

我有一个包含以下字段的定价表:

id
start_date
price

我想获得每个季度的第一个价格,我该如何实现?

我试过这个:

SELECT id,price,EXTRACT(YEAR FROM start_date::TIMESTAMP) AS year, EXTRACT(QUARTER FROM start_date::TIMESTAMP) AS quarter,min(start_date) as start_date
FROM pricing
GROUP BY id,price,EXTRACT(YEAR FROM start_date::TIMESTAMP), EXTRACT(QUARTER FROM start_date::TIMESTAMP)

但是如果价格发生变化,我会得到所有季度的价格,我只需要每个季度的第一个价格。

谢谢!

4

1 回答 1

0

可能有数百种方法可以实现它。您可以首先在一个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
于 2021-03-17T11:14:58.380 回答