---
title: "Querying DateTimes in ClickHouse"
description: "ClickHouse's datetime functions cover rounding, filtering and duration calculations, demonstrated on about 3 million NYC taxi trips. toStartOfHour and toStartOfFifteenMinutes bucket timestamps, casting to ::Time groups or filters by time of day across all dates, and dateDiff computes trip durations to derive average speed, which falls from about 19 mph at 6am to about 11 mph by 8:30am. countIf with toDayOfWeek splits weekday and weekend trips, and the lag window function computes percentage change between windows. formatDateTime and bar make the output readable."
url: "https://swe.quest/articles/querying-datetimes-in-clickhouse-2cdba42b9ef08c40"
canonical: "https://clickhouse.com/blog/querying-datetimes"
source: "ClickHouse"
published: "2026-03-20"
difficulty: "medium"
kind: "How-to"
reading_minutes: 8
tags: ["sql","analytics","time-clocks"]
---

# Querying DateTimes in ClickHouse

A post on [ClickHouse](https://swe.quest/sources/clickhouse.md), published 2026-03-20, 8 min read, How-to. Original: [https://clickhouse.com/blog/querying-datetimes](https://clickhouse.com/blog/querying-datetimes)

## Summary

ClickHouse's datetime functions cover rounding, filtering and duration calculations, demonstrated on about 3 million NYC taxi trips. toStartOfHour and toStartOfFifteenMinutes bucket timestamps, casting to ::Time groups or filters by time of day across all dates, and dateDiff computes trip durations to derive average speed, which falls from about 19 mph at 6am to about 11 mph by 8:30am. countIf with toDayOfWeek splits weekday and weekend trips, and the lag window function computes percentage change between windows. formatDateTime and bar make the output readable.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [SQL](https://swe.quest/tags/sql.md) (Languages)
- [Analytics & OLAP](https://swe.quest/tags/analytics.md) (Data & databases)
- [Time & clocks](https://swe.quest/tags/time-clocks.md) (Distributed systems)

## Prerequisites

- SQL basics ([easy posts on SQL](https://swe.quest/tags/sql.md?difficulty=easy))
- window functions

## Keywords

ClickHouse, NYC taxi dataset, dateDiff, toStartOfFifteenMinutes, MergeTree

## Related posts

- [Working with Time Series Data in ClickHouse](https://swe.quest/articles/working-with-time-series-data-in-clickhouse-d87ac55208de682a.md): ClickHouse · 2023-01-10 · Medium · 19 min read · Analytics & OLAP, SQL, Databases
  ClickHouse handles time-series workloads with Date, DateTime and DateTime64 types (up to nanosecond precision, with per-column timezones) plus SQL functions for bucketing by period. toStartOfHour and toStartOfInterval group by fixed or custom intervals, WITH FILL fills empty buckets, dateDiff builds offset rolling windows, and bar() gives quick inline visuals.
- [ClickHouse and the MTA Data Challenge](https://swe.quest/articles/clickhouse-and-the-mta-data-challenge-dc13e535ba1ed542.md): ClickHouse · 2024-10-24 · Medium · 16 min read · Analytics & OLAP, Data pipelines, SQL
  Loading and cleaning the ~95 million-row NYC MTA subway turnstile dataset (2014–2022) in ClickHouse, using schema inference over S3, a staging table, and an optimized schema with LowCardinality strings, a combined DateTime column, UInt32 counters, sorted line-name arrays and a (station, date\_time) ordering key.
- [What's New in pg\_clickhouse - JSONB Support, SQL value functions, Streaming, and more](https://swe.quest/articles/what-s-new-in-pg-clickhouse-jsonb-support-sql-value-functions-streaming-and-more-ecb202cf6ff32298.md): ClickHouse · 2026-04-24 · Medium · 10 min read · PostgreSQL, Analytics & OLAP, Query engines, Memory management
  pg\_clickhouse, the Postgres foreign data wrapper for querying ClickHouse, now pushes down far more operations to ClickHouse.
- [5 ways to parse Dates and DateTimes in ClickHouse](https://swe.quest/articles/5-ways-to-parse-dates-and-datetimes-in-clickhouse-af81329ee4e38796.md): ClickHouse · 2026-03-12 · Medium · 4 min read · Analytics & OLAP, SQL, Time & clocks
  ClickHouse offers five main routes for turning raw values into Date/DateTime types. \`fromUnixTimestamp\` and its 64Milli/64Micro variants convert epoch timestamps to \`DateTime\` or \`DateTime64(3/6)\`.
- [ClickHouse Newsletter August 2022: Airlines are maybe not that bad](https://swe.quest/articles/clickhouse-newsletter-august-2022-airlines-are-maybe-not-that-bad-1b3d45affdf6ad87.md): ClickHouse · 2022-08-11 · Medium · 7 min read · Analytics & OLAP, SQL, Databases
  ClickHouse's August 2022 newsletter introduces ClickBench, a benchmark for analytical databases built on a real-world web analytics dataset, and summarizes the 22.7 release: positional arguments on by default, deprecation of the Ordinary engine, new direct and full\_sorting\_merge join algorithms, a MongoDB table function, additional table and result filters, and HTTP support in clickhouse-go.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
