How to calculate the difference between two dates in ClickHouse

Learn how to calculate the difference between two dates in ClickHouse using the powerful date_diff function.

Photo: Propel

Discover the power of ClickHouse's date functions to manipulate dates with precision and ease

What is the <span class="code-exp-header">date_diff</span> function?

ClickHouse provides a range of powerful date and time functions that help you manipulate date and time data types in various ways. The <span class="code-exp">date_diff</span> function is a versatile function that enables you to calculate the differences between two dates or timestamps.

The <span class="code-exp">date_diff</span> function has the following syntax,

date_diff('<unit>', <startdate>, <enddate>)

where

  • <span class="code-exp-bracket">unit</span> is the interval for the result, such as “day”, “hour”, “minute”, “second”, etc.
  • <span class="code-exp-bracket">startdate</span> is the first time value to subtract (the subtrahend).
  • <span class="code-exp-bracket">enddate</span> is the second time value to subtract from (the minuend).

How to calculate the difference between two dates in ClickHouse SQL

You can calculate the difference between two dates in ClickHouse using the <span class="code-exp">date_diff</span> function.

For example, if you want to calculate the difference in days between two dates, the syntax would be:

SELECT date_diff('day', toDate('2021-12-29'), toDate('2022-01-01'))

This would return the number of days between December 29, 2021 and January 1, 2022.

Example: TacoSoft date manipulation

Let's look at an example using TacoSoft, our imaginary SaaS app to manage taquerias. Suppose you have a table called <span class="code-exp">orders</span> with the following columns:

  • <span class="code-exp">order_id</span>: Unique identifier for each order.
  • <span class="code-exp">order_date</span>: The date when the order was placed.
  • <span class="code-exp">customer_id</span>: Unique identifier for each customer.

Your task is to retrieve a list of all orders and the number of days between the order date and a specific date. In this case, let's say you want to find the number of days between the order date and April 14, 2023. You can use the <span class="code-exp">date_diff</span> function to calculate the difference between two dates in ClickHouse and achieve this goal.

Here is the SQL query for this task:

SELECT
  order_id,
  order_date,
  customer_id,
  date_diff('day', order_date, toDate('2023-04-14')) as days_diff
FROM
  orders;

This query would return all the rows in the <span class="code-exp">orders</span> table along with the number of days between the order date and April 14, 2023.

Advanced Example: Calculating Customer Retention with ClickHouse SQL

For a more advanced application of the <span class="code-exp">date_diff</span> function, let's look at how you can use it to calculate customer retention in our imaginary SaaS app, TacoSoft.

Suppose you want to find out how many days it has been since a customer's last order. This information can be useful for understanding customer retention and identifying customers who may be at risk of churning.

To do this, you need to calculate the difference between the current date and the maximum <span class="code-exp">order_date</span> for each <span class="code-exp">customer_id</span>. Here's how you can do this with the <span class="code-exp">date_diff</span> function:

SELECT
  customer_id,
  date_diff('day', MAX(order_date), toDate('2023-04-14')) AS days_since_last_order
FROM
  orders
GROUP BY
  customer_id;

In this query, the <span class="code-exp">MAX(order_date)</span> function retrieves the most recent <span class="code-exp">order_date</span> for each <span class="code-exp">customer_id</span>. The <span class="code-exp">date_diff</span> function then calculates the difference in days between this date and April 14, 2023.

This gives you a list of all customers and the number of days since their last order, which can be a valuable tool in your customer retention strategy.

Build faster with Propel: A Serverless Clickhouse for developers

At Propel, we offer a fully managed ClickHouse service that allows you to focus more on drawing insights from your data and less on infrastructure management. Propel provides an API for data access, React components, and built-in multi-tenant access controls, making it easier and faster for you to build data-intensive applications.

You can connect your own ClickHouse with Propel, whether it's self-hosted or on the ClickHouse Cloud, or take advantage of our fully managed serverless cloud.

Connect your own ClickHouse



✅ Works with self-hosted ClickHouse.
✅ Works with ClickHouse Cloud.
✅ Full control of your ClickHouse deployment.
✅ Data stays in your infrastructure.
✅ Data is encrypted in transit.
✅ Only pay for data passthrough.

Read the docs

Fully managed serverless cloud



✅ Ingest data from any data source.
✅ No infrastructure to manage or scale.
✅ Serverless auto-scaling.
✅ Mission-critical availability.
✅ Unlimited storage.
✅ Data is encrypted at rest and in transit.
✅ Only pay for the storage you use and queries you make.

Read the docs

Conclusion

In this post, we explored ClickHouse's <span class="code-exp">date_diff</span> function to calculate the difference between two dates or timestamps. By using this function, you can easily perform date difference calculations. This tool can be valuable for filtering and analyzing time-based data in your ClickHouse data warehouse.

ClickHouse offers many other date and time functions to help you manipulate and work with date and time data types. Don't hesitate to explore the official ClickHouse documentation to learn more about these functions and how to use them effectively.

Further Reading

For more insights on how to use ClickHouse for your data operations, including other date and time functions, check out our other posts. We cover a wide range of topics from advanced querying techniques to performance tuning for large datasets. Whether you're a beginner or an experienced data professional, there's always something new to learn!

Related posts

What's new in ClickHouse version 24.10

This is some text inside of a div block.

Heading 1

Heading 2

Heading 3

Heading 4

Heading 5
Heading 6

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.

Block quote

Ordered list

  1. Item 1
  2. Item 2
  3. Item 3

Unordered list

  • Item A
  • Item B
  • Item C

Text link

Bold text

Emphasis

Superscript

Subscript

Funnel analysis in ClickHouse

This is some text inside of a div block.

Heading 1

Heading 2

Heading 3

Heading 4

Heading 5
Heading 6

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.

Block quote

Ordered list

  1. Item 1
  2. Item 2
  3. Item 3

Unordered list

  • Item A
  • Item B
  • Item C

Text link

Bold text

Emphasis

Superscript

Subscript

What's new in ClickHouse version 24.9

This is some text inside of a div block.

Heading 1

Heading 2

Heading 3

Heading 4

Heading 5
Heading 6

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.

Block quote

Ordered list

  1. Item 1
  2. Item 2
  3. Item 3

Unordered list

  • Item A
  • Item B
  • Item C

Text link

Bold text

Emphasis

Superscript

Subscript

Start shipping today

Deliver the analytics your customers have been asking for.