在 PostgreSQL 中,同样可以使用 CTE(公共表表达式)来获取特定时间段的数据。PostgreSQL 提供了丰富的日期和时间函数,结合这些函数,能够轻松生成最近的月份、周、季度等时间段。下面是如何在 PostgreSQL 中获取最近十二个月、最近十二周、最近四个季度,以及十二个月前的月第一天和十二周前的周第一天的示例。
1. 获取最近十二个月
在 PostgreSQL 中,可以使用 TO_CHAR
函数结合递归CTE来生成最近十二个月的列表。
sql
WITH RECURSIVE last_twelve_months AS (
SELECT
TO_CHAR(CURRENT_DATE, 'YYYY-MM') AS year_month,
CURRENT_DATE::DATE AS date_value
UNION ALL
SELECT
TO_CHAR(date_value - INTERVAL '1 MONTH', 'YYYY-MM'),
(date_value - INTERVAL '1 MONTH')::DATE
FROM last_twelve_months
WHERE date_value > CURRENT_DATE - INTERVAL '11 MONTH'
)
SELECT year_month
FROM last_twelve_months
ORDER BY year_month DESC;
获取最近十二周
sql
WITH RECURSIVE last_twelve_weeks AS (
SELECT
TO_CHAR(DATE_TRUNC('week', CURRENT_DATE), 'YYYY-MM-DD') AS week_start,
DATE_TRUNC('week', CURRENT_DATE)::DATE AS date_value
UNION ALL
SELECT
TO_CHAR(DATE_TRUNC('week', date_value - INTERVAL '1 WEEK'), 'YYYY-MM-DD'),
DATE_TRUNC('week', date_value - INTERVAL '1 WEEK')::DATE
FROM last_twelve_weeks
WHERE date_value > CURRENT_DATE - INTERVAL '11 WEEK'
)
SELECT week_start
FROM last_twelve_weeks
ORDER BY week_start DESC;
获取最近四个季度
sql
WITH RECURSIVE last_four_quarters AS (
SELECT
EXTRACT(YEAR FROM CURRENT_DATE) AS year,
EXTRACT(QUARTER FROM CURRENT_DATE) AS quarter,
CURRENT_DATE::DATE AS date_value
UNION ALL
SELECT
CASE WHEN quarter = 1 THEN year - 1 ELSE year END,
CASE WHEN quarter = 1 THEN 4 ELSE quarter - 1 END,
(date_value - INTERVAL '3 MONTH')::DATE
FROM last_four_quarters
WHERE date_value > CURRENT_DATE - INTERVAL '1 YEAR'
)
SELECT CONCAT(year, '-Q', quarter) AS year_quarter
FROM last_four_quarters
ORDER BY year DESC, quarter DESC;
- 获取十二个月前的月第一天
sql
SELECT DATE_TRUNC('month', CURRENT_DATE - INTERVAL '12 MONTH') AS first_day_of_month;
- 获取十二周前的周第一天
sql
SELECT DATE_TRUNC('week', CURRENT_DATE - INTERVAL '12 WEEK') AS first_day_of_week;
测试和调试
- 确保在执行这些查询之前,检查 PostgreSQL 的版本,因为有些功能(如递归 CTE)在非常旧的版本中可能不支持。
- 如果仍然无法正确获取数据,可以逐步调试,如单独运行每一部分的查询,检查
CURRENT_DATE
、INTERVAL
的使用是否正确。 - 还可以直接使用
NOW()
替代CURRENT_DATE
来确保取到的是完整的时间戳数据。