摘要:在本教程中,您将学习如何使用 SQL DATE_TRUNC 函数来截断日期、时间或时间戳。
SQL DATE_TRUNC 函数简介 #
DATE_TRUNC 函数允许您将日期、时间或时间戳截断到指定的精度。
以下是 DATE_TRUNC 函数的语法:
DATE_TRUNC(unit, date)Code language: PostgreSQL SQL dialect and PL/pgSQL (pgsql)在此语法中:
unit:您想要截断日期的时间单位。单位可以是YEAR(年)、MONTH(月)、DAY(日)、HOUR(小时)、MINUTE(分钟)、SECOND(秒)等。date:您想要截断的日期、时间或时间戳。
DATE_TRUNC 函数对于时间序列分析和数据聚合非常有用。
SQL DATE_TRUNC 函数示例 #
让我们使用 HR 示例数据库中的 employees 表来演示 DATE_TRUNC 函数。

截断到年初 #
以下语句使用 DATE_TRUNC 函数将 employees 表中 hire_date 列的值截断到当年的年初:
SELECT
employee_id,
first_name,
last_name,
hire_date,
DATE_TRUNC('YEAR', hire_date) AS hire_year_start
FROM
employees
ORDER BY
hire_year_start;Code language: PostgreSQL SQL dialect and PL/pgSQL (pgsql)输出
employee_id | first_name | last_name | hire_date | hire_year_start
-------------+-------------+-------------+------------+-----------------
200 | Jennifer | Whalen | 1987-09-17 | 1987-01-01
100 | Steven | King | 1987-06-17 | 1987-01-01
101 | Neena | Kochhar | 1989-09-21 | 1989-01-01
103 | Alexander | Hunold | 1990-01-03 | 1990-01-01
...Code language: PostgreSQL SQL dialect and PL/pgSQL (pgsql)该查询返回员工 ID、名字、姓氏、原始入职日期以及入职日期的年初。
您可以使用 hire_year_start 列的数据来计算每年年初的员工总数。
按年初对员工进行分组 #
以下语句使用 DATE_TRUNC 函数将入职日期截断到年初,并结合 GROUP BY 子句和 COUNT 函数来计算每个年初的员工总数:
SELECT
DATE_TRUNC('YEAR', hire_date) AS hire_year_start,
COUNT(*) AS headcount
FROM
employees
GROUP BY
hire_year_start
ORDER BY
hire_year_start;Code language: PostgreSQL SQL dialect and PL/pgSQL (pgsql)输出
hire_year_start | headcount
-----------------+-----------
1987-01-01 | 2
1989-01-01 | 1
1990-01-01 | 1
1991-01-01 | 1
1993-01-01 | 1
1994-01-01 | 7
1995-01-01 | 2
1996-01-01 | 4
1997-01-01 | 10
1998-01-01 | 6
1999-01-01 | 4
2000-01-01 | 1Code language: PostgreSQL SQL dialect and PL/pgSQL (pgsql)截断到月初 #
以下示例使用 DATE_TRUNC 函数将 hire_date 截断到月初:
SELECT
employee_id,
first_name,
last_name,
hire_date,
DATE_TRUNC('MONTH', hire_date) AS hire_month_start
FROM
employees
ORDER BY
hire_month_start;Code language: PostgreSQL SQL dialect and PL/pgSQL (pgsql)输出
employee_id | first_name | last_name | hire_date | hire_month_start
-------------+-------------+-------------+------------+------------------
100 | Steven | King | 1987-06-17 | 1987-06-01
200 | Jennifer | Whalen | 1987-09-17 | 1987-09-01
101 | Neena | Kochhar | 1989-09-21 | 1989-09-01
103 | Alexander | Hunold | 1990-01-03 | 1990-01-01
104 | Bruce | Ernst | 1991-05-21 | 1991-05-01
...Code language: PostgreSQL SQL dialect and PL/pgSQL (pgsql)该查询返回员工 ID、名字、姓氏、原始入职日期以及入职日期的月初。
您可以使用 hire_month_start 列中的数据来计算从入职月份初开始计入工资单的新员工数量。
按入职月份对员工进行分组 #
以下语句使用 DATE_TRUNC 按入职月份初统计员工人数:
SELECT
DATE_TRUNC('MONTH', hire_date)::DATE AS hire_month,
COUNT(*) AS new_employee_count
FROM
employees
GROUP BY
hire_month
ORDER BY
hire_month;Code language: PostgreSQL SQL dialect and PL/pgSQL (pgsql)输出
hire_month | new_employee_count
------------+--------------------
1987-06-01 | 1
1987-09-01 | 1
1989-09-01 | 1
1990-01-01 | 1
1991-05-01 | 1
1993-01-01 | 1
1994-06-01 | 4
1994-08-01 | 2
...Code language: PostgreSQL SQL dialect and PL/pgSQL (pgsql)摘要 #
- 使用 SQL
DATE_TRUNC函数将日期和时间截断到指定的精度。
本教程是否有帮助?