GithubHelp home page GithubHelp logo

zacharyrankin / groupdate.sql Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ankane/groupdate.sql

0.0 2.0 0.0 120 KB

The simplest way to group temporal data

Home Page: http://ankane.github.io/groupdate.sql

License: MIT License

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 (complete list at bottom)

๐ŸŽ‰ Time zones supported!! the best part

๐ŸŒŽ Use it with any programming language the other best part

Supports PostgreSQL and MySQL

Usage

Groupdate.sql creates SQL functions, which are stored by the database.

You can use these functions with any ORM (or without one).

SQL

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 07:00:00+00 |   145
 2013-04-12 07:00:00+00 |    65
 2013-04-13 07:00:00+00 |   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 05:00:00+00 |  1327
 2013-05-12 05:00:00+00 |  3432
 2013-05-19 05:00:00+00 |   673

Group by day of week

SELECT gd_day_of_week(created_at, 'America/New_York') AS day_of_week, COUNT(*)
  FROM orders GROUP BY day_of_week;
-- result
 day_of_week | count
-------------+-------
           0 |   167
           1 |   273
           2 |   439
           3 |   285
           4 |   318
           5 |   123
           6 |   189

Note: Weeks start on Sunday.

Language / Frameworks

Ruby on Rails (activerecord)

User.group("gd_day(created_at)").count

Django, Node.js, Go, etc

# pull requests please :)

Installation

For PostgreSQL

curl https://raw.github.com/ankane/groupdate.sql/master/postgresql.sql | psql db_name

For MySQL

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

Time zone support must be installed on the server.

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

Complete list

All functions are prefixed with gd_ to prevent conflicts with MySQL functions.

  • second
  • minute
  • hour
  • day
  • week
  • month
  • year
  • hour_of_day
  • day_of_week

A list of time zones can be found here

Uninstall

For PostgreSQL

curl https://raw.github.com/ankane/groupdate.sql/master/uninstall_postgresql.sql | psql db_name

For MySQL

curl https://raw.github.com/ankane/groupdate.sql/master/uninstall_mysql.sql | mysql db_name

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

groupdate.sql's People

Contributors

ankane avatar

Watchers

 avatar  avatar

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.