Skip to content

Instantly share code, notes, and snippets.

@jaimerson
Created August 1, 2016 21:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaimerson/ba1aa484b1d5e8d6e708b75620fab0e2 to your computer and use it in GitHub Desktop.
Save jaimerson/ba1aa484b1d5e8d6e708b75620fab0e2 to your computer and use it in GitHub Desktop.
Materialized view with scenic
class CreateBikesReports < ActiveRecord::Migration
def change
create_view :bikes_reports, materialized: true
end
end
SELECT
bikes.name, bikes.model,
count(rentals.id) as times_rented,
round(SUM(extract(
epoch from (rentals.returned_at - rentals.created_at)
)/ 3600 * bikes.price)::numeric, 2) as revenue
FROM
rentals INNER JOIN bikes ON bikes.id = rentals.bike_id
GROUP BY bikes.id
ORDER BY times_rented DESC;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment