All Projects → ankane → Groupdate.sql

ankane / Groupdate.sql

Licence: mit
The simplest way to group temporal data

Labels

Projects that are alternatives of or similar to Groupdate.sql

3dcitydb
3D City Database - The Open Source CityGML Database
Stars: ✭ 210 (-19.23%)
Mutual labels:  plpgsql
Xtuple
This repository contains the source code for the database schema for the PostBooks edition of xTuple ERP and xTuple's REST API server. The REST API server is written in JavaScript running on Node.js. The database schema for PostBooks runs on a PostgreSQL database server.
Stars: ✭ 247 (-5%)
Mutual labels:  plpgsql
pmts
Poor man's time series functionality for PostgreSQL
Stars: ✭ 31 (-88.08%)
Mutual labels:  plpgsql
Dbt Utils
Utility functions for dbt projects.
Stars: ✭ 212 (-18.46%)
Mutual labels:  plpgsql
Pg Coordtransform
基于PostgreSQL+PostGIS的火星坐标系、百度坐标系、WGS84坐标系、CGCS2000坐标系的转换函数
Stars: ✭ 224 (-13.85%)
Mutual labels:  plpgsql
hashids.sql
PL/pgSQL implementation of hashids library
Stars: ✭ 40 (-84.62%)
Mutual labels:  plpgsql
Pgjwt
PostgreSQL implementation of JWT (JSON Web Tokens)
Stars: ✭ 198 (-23.85%)
Mutual labels:  plpgsql
Bedquilt Core
A JSON document store on PostgreSQL
Stars: ✭ 256 (-1.54%)
Mutual labels:  plpgsql
Pgmonitor
PostgreSQL Monitoring, Metrics Collection and Alerting Resources from Crunchy Data
Stars: ✭ 235 (-9.62%)
Mutual labels:  plpgsql
phpPgAdmin6
PHP7+ Based administration tool for PostgreSQL 9.3+
Stars: ✭ 45 (-82.69%)
Mutual labels:  plpgsql
Freedom
capstone based disassembler for extracting to binnavi
Stars: ✭ 214 (-17.69%)
Mutual labels:  plpgsql
Plsm
Generates Ecto models from already existing tables
Stars: ✭ 221 (-15%)
Mutual labels:  plpgsql
stacker.news
It's like Hacker News but we pay you Bitcoin.
Stars: ✭ 196 (-24.62%)
Mutual labels:  plpgsql
Store
PostgreSQL shopping cart
Stars: ✭ 213 (-18.08%)
Mutual labels:  plpgsql
sql-surveyor
High-level SQL parser. Identify tables, columns, aliases and more from your SQL script in one easy to consume object. Supports PostgreSQL, MySQL, SQL Server and Oracle (PL/SQL) dialects.
Stars: ✭ 19 (-92.69%)
Mutual labels:  plpgsql
Schemaverse
The Schemaverse is a space-based strategy game implemented entirely within a PostgreSQL database. Compete against other players using raw SQL commands to command your fleet. Or, if your PL/pgSQL-foo is strong, wield it to write AI and have your fleet command itself!
Stars: ✭ 207 (-20.38%)
Mutual labels:  plpgsql
id3c
Data logistics system enabling real-time pathogen surveillance. Built for the Seattle Flu Study.
Stars: ✭ 21 (-91.92%)
Mutual labels:  plpgsql
Pgmemento
Audit trail with schema versioning for PostgreSQL using transaction-based logging
Stars: ✭ 260 (+0%)
Mutual labels:  plpgsql
Openopps Platform
Open Opportunities open source platform
Stars: ✭ 255 (-1.92%)
Mutual labels:  plpgsql
mimeo
Extension for specialized, per-table replication between PostgreSQL instances
Stars: ✭ 74 (-71.54%)
Mutual labels:  plpgsql

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 and MySQL

Build Status

Installation

Run this SQL

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

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

or copy and paste these statements into a SQL console.

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
------------+-------
 2013-04-11 |   145
 2013-04-12 |    65
 2013-04-13 |   293

Group by week

SELECT gd_week(created_at, 'America/Chicago') AS week, COUNT(*) FROM users GROUP BY week;
-- result
    week    | count
------------+-------
 2013-05-05 |  1327
 2013-05-12 |  3432
 2013-05-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, run:

START TRANSACTION;

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

COMMIT;

Uninstall

Run this SQL

Contributing

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].