https://github.com/umbrellio/sequel-tablefunc
Sequel extension for crosstabs
https://github.com/umbrellio/sequel-tablefunc
Last synced: 12 days ago
JSON representation
Sequel extension for crosstabs
- Host: GitHub
- URL: https://github.com/umbrellio/sequel-tablefunc
- Owner: umbrellio
- Created: 2018-01-31T20:21:50.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-02-08T07:56:03.000Z (over 8 years ago)
- Last Synced: 2025-01-03T12:14:07.503Z (over 1 year ago)
- Language: Ruby
- Size: 16.6 KB
- Stars: 2
- Watchers: 5
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Sequel::Tablefunc [](https://travis-ci.org/fiscal-cliff/sequel-tablefunc) [](https://badge.fury.io/rb/sequel-tablefunc) [](https://coveralls.io/github/fiscal-cliff/sequel-tablefunc?branch=master)
This is sequel extension makes using crosstab function more convenient
## Installation
Add this line to your application's Gemfile:
```ruby
gem 'sequel-tablefunc'
```
And then execute:
$ bundle
## Usage
Assuming you have table which already have 2 categories. This table can be represented as a pivot table
```ruby
User.select_group(:type_id, :status_id)
.select_append(:count.sql_function.*)
.order(:type_id)
.crosstab(User.select(:status_id).distinct.order(:status_id))
.all
```
| row_name | status1 | status2 |
| :------------- | :------------- | :------------- |
| type1 | 5 | 10|
| type2 | 1 | 2|
It is easy, isn't it?
```ruby
User.select_group(:date_trunc.sql_function('year', :created_at), :status_id)
.select_append(:count.sql_function.*)
.order(:date_trunc.sql_function('year', :created_at))
.crosstab(User.select(:status_id)
.distinct
.order(:status_id))
.all
```
| row_name | status1 | status2 |
| :------------- | :------------- | :------------- |
| 2012-01-01 00:00:00 | 6 | nil|
| 2013-01-01 00:00:00 | nil | 12|
## Development
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/umbrellio/sequel-tablefunc.