GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com โ€” GitHub - ankane/groupdate.sql: The simplest way to group temporal data

BranchesTags
Open more actions menu

Latest commit

 

History

99 Commits
99 Commits

Folders and files

NameName
Last commit message
Last commit date
.github/workflows
.github/workflows
 
 
mysql
mysql
 
 
postgresql
postgresql
 
 
test
test
 
 
.gitignore
.gitignore
 
 
CHANGELOG.md
CHANGELOG.md
 
 
Gemfile
Gemfile
 
 
LICENSE.txt
LICENSE.txt
 
 
README.md
README.md
 
 
Rakefile
Rakefile
 
 

Repository files navigation

Groupdate.sql

The simplest way to group by:

  • day
  • week
  • month
  • day of the week
  • hour of the day
  • and more

๐ŸŽ‰ Time zones supported!!

Supports PostgreSQL, MySQL, and MariaDB

Build Status

Installation

Run this SQL

  • PostgreSQL
  • MySQL and MariaDB

For MySQL and MariaDB, time zone support must be installed on the server.

For MySQL, use:

mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root mysql

For MariaDB, use:

mariadb-tzinfo-to-sql /usr/share/zoneinfo | mariadb -u root mysql

Getting Started

Groupdate.sql creates SQL functions, which are stored by the database. You can use these functions with any ORM or without one. Times stored in your database are assumed to be UTC (this is the default for frameworks like Rails).

Group by day

SELECT gd_day(created_at, 'America/Los_Angeles') AS day, COUNT(*) FROM users GROUP BY day;
-- result
    day     | count
------------+-------
 2025-01-01 |   145
 2025-01-02 |    65
 2025-01-03 |   293

Group by week

SELECT gd_week(created_at, 'America/Chicago') AS week, COUNT(*) FROM users GROUP BY week;
-- result
    week    | count
------------+-------
 2025-01-05 |  1327
 2025-01-12 |  3432
 2025-01-19 |   673

Weeks start on Sunday by default

Group by dynamic period

SELECT gd_period('day', created_at, 'America/Los_Angeles') AS period, COUNT(*) FROM users GROUP BY period;

Works with day, week, month, and year

Functions

Hereโ€™s the complete list of functions. All are prefixed with gd_ to prevent conflicts with MySQL functions.

  • second
  • minute
  • hour
  • day
  • week
  • month
  • quarter
  • year
  • hour_of_day
  • day_of_week
  • day_of_month
  • day_of_year
  • month_of_year

A list of time zones can be found here.

Options

Time Zone

With PostgreSQL, the time zone is optional.

SELECT gd_day(created_at) AS day, COUNT(*) FROM users GROUP BY day;

The default is UTC. Change this to your preferred time zone by running:

CREATE OR REPLACE FUNCTION gd_time_zone()
  RETURNS text AS
$$
  SELECT 'America/Los_Angeles'::text;
$$
  LANGUAGE SQL IMMUTABLE;

Week Start

Set the day weeks start. The default is Sunday.

For PostgreSQL, run:

CREATE OR REPLACE FUNCTION gd_week_start()
  RETURNS int AS
$$
  SELECT 6; -- mon=0, tue=1, wed=2, thu=3, fri=4, sat=5, sun=6
$$
  LANGUAGE SQL IMMUTABLE;

For MySQL and MariaDB, run:

START TRANSACTION;

DROP FUNCTION IF EXISTS gd_week_start;
CREATE FUNCTION gd_week_start()
  RETURNS INT DETERMINISTIC
  RETURN 6; -- mon=0, tue=1, wed=2, thu=3, fri=4, sat=5, sun=6

COMMIT;

Uninstall

Run this SQL

  • PostgreSQL
  • MySQL and MariaDB

History

View the changelog

Contributing

Everyone is encouraged to help improve this project. Here are a few ways you can help:

  • Report bugs
  • Fix bugs and submit pull requests
  • Write, clarify, or fix documentation
  • Suggest or add new features

To get started with development:

git clone https://github.com/ankane/groupdate.sql.git
cd groupdate.sql
bundle install
bundle exec rake test

About

The simplest way to group temporal data

Resources

Readme
MIT license
Activity

Stars

286 stars

Watchers

7 watching

Forks

17 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402503 โ€” GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800ร—600. No cookies required.