- StarRocks
- Quick Start
- Table Design
- Data Loading
- Data Export
- Using StarRocks
- Reference
- SQL Reference
- User Account Management
- Cluster Management
- ADMIN CANCEL REPAIR
- ADMIN CHECK TABLET
- ADMIN REPAIR
- ADMIN SET CONFIG
- ADMIN SET REPLICA STATUS
- ADMIN SHOW CONFIG
- ADMIN SHOW REPLICA DISTRIBUTION
- ADMIN SHOW REPLICA STATUS
- ALTER SYSTEM
- CANCEL DECOMMISSION
- CREATE FILE
- DROP FILE
- INSTALL PLUGIN
- SHOW BACKENDS
- SHOW BROKER
- SHOW FRONTENDS
- SHOW FULL COLUMNS
- SHOW INDEX
- SHOW PLUGINS
- SHOW TABLE STATUS
- SHOW FILE
- UNINSTALL PLUGIN
- DDL
- ALTER DATABASE
- ALTER TABLE
- ALTER VIEW
- ALTER RESOURCE
- BACKUP
- CANCEL BACKUP
- CANCEL RESTORE
- CREATE DATABASE
- CREATE INDEX
- CREATE MATERIALIZED VIEW
- CREATE REPOSITORY
- CREATE RESOURCE
- CREATE TABLE AS SELECT
- CREATE TABLE LIKE
- CREATE TABLE
- CREATE VIEW
- CREATE FUNCTION
- DROP DATABASE
- DROP INDEX
- DROP MATERIALIZED VIEW
- DROP REPOSITORY
- DROP RESOURCE
- DROP TABLE
- DROP VIEW
- DROP FUNCTION
- HLL
- RECOVER
- RESTORE
- SHOW RESOURCES
- SHOW FUNCTION
- TRUNCATE TABLE
- DML
- ALTER ROUTINE LOAD
- BROKER LOAD
- CANCEL LOAD
- DELETE
- EXPORT
- GROUP BY
- INSERT
- PAUSE ROUTINE LOAD
- RESUME ROUTINE LOAD
- ROUTINE LOAD
- SELECT
- SHOW ALTER
- SHOW BACKUP
- SHOW DATA
- SHOW DATABASES
- SHOW DELETE
- SHOW DYNAMIC PARTITION TABLES
- SHOW EXPORT
- SHOW LOAD
- SHOW PARTITIONS
- SHOW PROPERTY
- SHOW REPOSITORIES
- SHOW RESTORE
- SHOW ROUTINE LOAD
- SHOW ROUTINE LOAD TASK
- SHOW SNAPSHOT
- SHOW TABLES
- SHOW TABLET
- SHOW TRANSACTION
- SPARK LOAD
- STOP ROUTINE LOAD
- STREAM LOAD
- Data Types
- Auxiliary Commands
- Function Reference
- Date Functions
- Geographic Functions
- String Functions
- JSON Functions
- Overview of JSON functions and operators
- JSON constructor functions
- JSON query and processing functions
- JSON operators
- Aggregate Functions
- Bitmap Functions
- Array Functions
- cast function
- hash function
- Cryptographic Functions
- Math Functions
- Utility Functions
- System variables
- Error code
- System limits
- SQL Reference
- Administration
- FAQ
- Deploy
- Data Migration
- SQL
- Other FAQs
- Benchmark
- Release Notes
date_trunc
Background
When using the time function, users often need to group time by hour / week and want to display it directly in time format and in the first column. They need a function similar to oracle trunc to effectively truncate the datetime so as to avoid writing sql as ineffective as below:
select ADDDATE(DATE_FORMAT(DATE_ADD(from_unixtime(`timestamp`), INTERVAL 8 HOUR),
'%Y-%m-%d %H:%i:00'),
INTERVAL 0 SECOND),
count()
from xxx group 1 ;
Motive
To truncate time more efficiently and to provide vectorized date_trunc functions.
We already have time functions that directly truncate year/month/day.
Similarly, we truncate the high bits of datetime using data_trunc
date_trunc("minute", datetime):
2020-11-04 11:12:19 => 2020-11-04 11:12:00
Function Signature
- Oracle uses the function format of TRUNC(date,[fmt]).
- PostgreSQL/redshift uses the function format of date_trunc(text,time).
- StarRocks uses the function format of date_trunc([fmt], datetime).
Function Implementation
Take two easy steps to create date_trunc:
- Break down the datetime into small parts (Year, Month, Day/Hour, Minute, Second) and extract the parts you need
- Based on the parts extracted, create a new datetime
Special calculation is needed for week/quarter.
Also, you need to study the week in snowflake.
--set the first day of the week in snowflake
--Set Monday as the first day of the week for these two according to the default.
alter session set week_start = 0
alter session set week_start = 1
--Set Wednesday as the first day of the week
alter session set week_start = 3
description
Syntax
DATETIME date_trunc(VARCHAR fmt, DATETIME datetime)
Truncate datetime into fmt format.
Fmt supports string literals. But they must be several fixed values: second, minute, hour, day, month, year, week, quarter). Wrong values inputted will be returned as error information by FE analysis.
The string literals inputted in datetime will be identified. This process happens only once.
strings in fmt format Meaning example second round down to second 2020-10-25 11:15:32 => 2020-10-25 11:15:32 minute round down to minute 2020-11-04 11:12:19 => 2020-11-04 11:12:00 hour round down to hour 2020-11-04 11:12:13 => 2020-11-04 11:00:00 day round down to day 2020-11-04 11:12:05 => 2020-11-04 00:00:00 month round down to the first day of the month 2020-11-04 11:12:51 => 2020-11-01 00:00:00 year round down to the first day of the year 2020-11-04 11:12:00 => 2020-01-01 00:00:00 week round down to the first day of the week 2020-11-04 11:12:00 => 2020-11-02 00:00:00 quarter round down to the first day of the quarter 2020-06-23 11:12:00 => 2020-04-01 00:00:00
example
MySQL > select date_trunc("hour", "2020-11-04 11:12:13")
2020-11-04 11:00:00
keyword
DATE_TRUNC,DATE