0

I am not sure why I am getting the syntax error, according to this question everything should be okay. I could not find a way to execute a window function (postgres query which returns multiple columns GROUPED BY an individual column) that is the reason why I used raw SQL.

I am still new to Postgres, but I gathered that it may have to do with sanitization, but I do not know how to go about that either. Any help will be greatly appreciated, here is my code:

module TasksTestQuery

  def self.call

    query = "SELECT user_id, username, date, SUM(user_id) AS total_work, SUM(duration) AS total_duration OVER (PARTITION BY user_id) FROM tasks"

    ActiveRecord::Base.connection.find_by_sql(query)

  end
end

The exact error I'm getting is:

ActiveRecord::StatementInvalid
PG::SyntaxError: ERROR: syntax error at or near "(" LINE 1: SELECT user_id, username, date, SUM(user_id) AS total_work, SUM(duration) AS total_duration OVER (PARTITION BY user_id) FROM tasks

1 Answers1

0

It should be

SUM(duration) OVER (PARTITION BY user_id) AS total_duration
Laurenz Albe
  • 209,280
  • 17
  • 206
  • 263