GithubHelp home page GithubHelp logo

groupdate.sql's Introduction

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

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 DETERMINISTIC
  RETURN 6; -- mon=0, tue=1, wed=2, thu=3, fri=4, sat=5, sun=6

COMMIT;

Uninstall

Run this SQL

History

View the changelog

Contributing

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

groupdate.sql's People

Contributors

ankane avatar thomassnielsen avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

groupdate.sql's Issues

Windows Error

I apologize for asking this question as I'm on a Windows machine. I've installed this on my Macbook and it works brilliantly. But I'm having issues with Windows.

When running

$curl https://raw.github.com/ankane/groupdate.sql/master/mysql.sql | mysql [db_name]

I receive the following error

ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'HTTP/1.1 200 OK
Date: Sun, 18 Aug 2013 08:31:01 GMT
Server: GitHub.com
Content-T at line 1

Question about use of timezone function

Thank you for all these very useful udf's here (including the recent gd_quarter function too πŸŽ‰)

This might be a stupid question but I failed to see through the reason why we used CONVERT_TZ twice here for some of the functions. for ex:

CONVERT_TZ(DATE_FORMAT(CONVERT_TZ(ts, '+00:00', time_zone), '%Y-%m-%d 00:00:00'), time_zone, '+00:00');

once to convert timestamp to a desired timezone only to convert it back to UTC.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.