Created
August 1, 2016 21:28
-
-
Save jaimerson/ba1aa484b1d5e8d6e708b75620fab0e2 to your computer and use it in GitHub Desktop.
Materialized view with scenic
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CreateBikesReports < ActiveRecord::Migration | |
def change | |
create_view :bikes_reports, materialized: true | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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