Guest User

Untitled

a guest
Nov 16th, 2018
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. sql = <<-SQL
  2. SELECT SUM(price) as total_price,
  3. SUM(distance) as total_distance,
  4. TO_CHAR(date, 'YYYY-MM') as month
  5. FROM Rides
  6. WHERE user_id = #{current_user.id}
  7. GROUP_BY month
  8. SQL
  9.  
  10. Ride.find_by_sql(sql).each do |row|
  11. "#{row.month}: { total_distance: #{row.total_distance}, total_price: #{row.total_price} }"
  12. end
  13.  
  14. ActiveRecord::StatementInvalid: PG::SyntaxError: ERROR: syntax error at or near "GROUP_BY"
  15. LINE 6: GROUP_BY month
  16. ^
  17. : SELECT SUM(price) as total_price,
  18. SUM(distance) as total_distance,
  19. TO_CHAR(date, 'YYYY-MM') as month
  20. FROM Rides
  21. WHERE user_id = 1
  22. GROUP_BY month
  23.  
  24. str = "select sum(distance) as total_distance, sum(price) as total_price, to_char(date, 'YYYY-MM') as month from rides where user_id = #{ current_user.id } group by month"
Add Comment
Please, Sign In to add comment