Created
October 27, 2016 13:58
-
-
Save StaverDmitry/03ed92d63ef3c1e8dcdee1819e659134 to your computer and use it in GitHub Desktop.
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
def stats( params ) | |
params = params.with_indifferent_access | |
group_by = params[:group_by] || 'week' | |
cruise_name = params['cruise_name'] | |
query = "SELECT cruises.name, | |
DATE_TRUNC('#{group_by}', tours.departure) AS #{group_by}, | |
COUNT(enquiries.id) AS count_enquiries, | |
SUM(enquiries.pax) AS count_paxes, | |
SUM(CASE WHEN enquiries.booking_status='Confirmed' THEN enquiries.pax END) AS count_confirmed_paxes | |
FROM cruises | |
INNER JOIN enquiries ON cruises.id = enquiries.cruise_id | |
INNER JOIN tours ON enquiries.tour_id = tours.id\n" | |
cruise_name.present? ? query += "WHERE cruises.name = '#{ cruise_name }'\n" : "" | |
query += "GROUP BY #{group_by}, cruises.name ORDER BY week;" | |
ActiveRecord::Base.connection.execute( query ) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment