mirror of
https://github.com/postgres/postgres.git
synced 2025-07-31 22:04:40 +03:00
Add date_bin function
Similar to date_trunc, but allows binning by an arbitrary interval rather than just full units. Author: John Naylor <john.naylor@enterprisedb.com> Reviewed-by: David Fetter <david@fetter.org> Reviewed-by: Isaac Morland <isaac.morland@gmail.com> Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us> Reviewed-by: Artur Zakirov <zaartur@gmail.com> Discussion: https://www.postgresql.org/message-id/flat/CACPNZCt4buQFRgy6DyjuZS-2aPDpccRkrJBmgUfwYc1KiaXYxg@mail.gmail.com
This commit is contained in:
@ -8730,6 +8730,20 @@ SELECT regexp_match('abc01234xyz', '(?:(.*?)(\d+)(.*)){1,1}');
|
||||
</para></entry>
|
||||
</row>
|
||||
|
||||
<row>
|
||||
<entry role="func_table_entry"><para role="func_signature">
|
||||
<function>date_bin</function> ( <type>interval</type>, <type>timestamp</type>, <type>timestamp</type> )
|
||||
<returnvalue>timestamp</returnvalue>
|
||||
</para>
|
||||
<para>
|
||||
Bin input into specified interval aligned with specified origin; see <xref linkend="functions-datetime-bin"/>
|
||||
</para>
|
||||
<para>
|
||||
<literal>date_bin('15 minutes', timestamp '2001-02-16 20:38:40', timestamp '2001-02-16 20:05:00')</literal>
|
||||
<returnvalue>2001-02-16 20:35:00</returnvalue>
|
||||
</para></entry>
|
||||
</row>
|
||||
|
||||
<row>
|
||||
<entry role="func_table_entry"><para role="func_signature">
|
||||
<indexterm>
|
||||
@ -9868,6 +9882,42 @@ SELECT date_trunc('hour', INTERVAL '3 days 02:47:33');
|
||||
</para>
|
||||
</sect2>
|
||||
|
||||
<sect2 id="functions-datetime-bin">
|
||||
<title><function>date_bin</function></title>
|
||||
|
||||
<indexterm>
|
||||
<primary>date_bin</primary>
|
||||
</indexterm>
|
||||
|
||||
<para>
|
||||
The function <function>date_bin</function> <quote>bins</quote> the input
|
||||
timestamp into the specified interval (the <firstterm>stride</firstterm>)
|
||||
aligned with a specified origin.
|
||||
</para>
|
||||
|
||||
<para>
|
||||
Examples:
|
||||
<screen>
|
||||
SELECT date_bin('15 minutes', TIMESTAMP '2020-02-11 15:44:17', TIMESTAMP '2001-01-01');
|
||||
<lineannotation>Result: </lineannotation><computeroutput>2020-02-11 15:30:00</computeroutput>
|
||||
|
||||
SELECT date_bin('15 minutes', TIMESTAMP '2020-02-11 15:44:17', TIMESTAMP '2001-01-01 00:02:30');
|
||||
<lineannotation>Result: </lineannotation><computeroutput>2020-02-11 15:32:30</computeroutput>
|
||||
</screen>
|
||||
</para>
|
||||
|
||||
<para>
|
||||
In cases full units (1 minute, 1 hour, etc.), it gives the same result as
|
||||
the analogous <function>date_trunc</function> call, but the difference is
|
||||
that <function>date_bin</function> can truncate to an arbitrary interval.
|
||||
</para>
|
||||
|
||||
<para>
|
||||
The <parameter>stride</parameter> interval cannot contain units of month
|
||||
or larger.
|
||||
</para>
|
||||
</sect2>
|
||||
|
||||
<sect2 id="functions-datetime-zoneconvert">
|
||||
<title><literal>AT TIME ZONE</literal></title>
|
||||
|
||||
|
Reference in New Issue
Block a user