Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4588084
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T21:52:20+00:00 2026-05-21T21:52:20+00:00

Heroku throws an error on my Postgres-Query stating: ActiveRecord::StatementInvalid (PGError: ERROR: syntax error at

  • 0

Heroku throws an error on my Postgres-Query stating:

ActiveRecord::StatementInvalid
(PGError: ERROR: syntax error at or
near “date” 2011-05-03T13:58:22+00:00
app[web.1]: LINE 1: …2011-05-31′)
GROUP BY EXTRACT(YEAR FROM TIMESTAMP
date)||EXT…

The SQLite query in development works as expected. Here is the code:

  def self.calculate(year, month, user_id, partner_id)
    case ActiveRecord::Base.connection.adapter_name  
      when 'SQLite'
        where(':user_id = entries.user_id OR :partner_id = entries.user_id', {    
            :user_id => user_id,
            :partner_id => partner_id
        }).
        where('entries.date <= :last_day', { 
            :last_day => Date.new(year, month, 1).at_end_of_month
        }).
        select('entries.date, ' + 
               'sum(case when joint = "f" then amount_calc else 0 end) as sum_private, ' + 
               'sum(case when joint = "t" and user_id = ' + user_id.to_s + ' then amount_calc else 0 end) as sum_user_joint, ' + 
               'sum(case when joint = "t" and user_id = ' + partner_id.to_s + ' then amount_calc else 0 end) as sum_partner_joint, ' +              
               'sum(case when compensation = "t" and user_id = ' + user_id.to_s + ' then amount_calc else 0 end) as sum_user_compensation, ' +
               'sum(case when compensation = "t" and user_id = ' + partner_id.to_s + ' then amount_calc else 0 end) as sum_partner_compensation '
        ).
        group("strftime('%Y-%m', date)")
      when 'PostgreSQL'
        where(':user_id = entries.user_id OR :partner_id = entries.user_id', {    
            :user_id => user_id,
            :partner_id => partner_id
        }).
        where('entries.date <= :last_day', { 
            :last_day => Date.new(year, month, 1).at_end_of_month
        }).
        select('entries.date, ' + 
               'sum(case when joint = "f" then amount_calc else 0 end) as sum_private, ' + 
               'sum(case when joint = "t" and user_id = ' + user_id.to_s + ' then amount_calc else 0 end) as sum_user_joint, ' + 
               'sum(case when joint = "t" and user_id = ' + partner_id.to_s + ' then amount_calc else 0 end) as sum_partner_joint, ' +              
               'sum(case when compensation = "t" and user_id = ' + user_id.to_s + ' then amount_calc else 0 end) as sum_user_compensation, ' +
               'sum(case when compensation = "t" and user_id = ' + partner_id.to_s + ' then amount_calc else 0 end) as sum_partner_compensation '
        ).
        group("EXTRACT(YEAR FROM TIMESTAMP date)||EXTRACT(MONTH FROM TIMESTAMP date)")
    else
      raise 'Query not implemented for this DB adapter'
    end
  end

I would really appreciate any hints. And as I am already asking a question here, I am uncertain about the case when joint = "t" in the sums in both queries too, is there a better way to do this?

UPDATE

Thanks to both peufeu and a horse with no name the code now looks like:

when 'PostgreSQL'
where(':user_id = entries.user_id OR :partner_id = entries.user_id', {    
    :user_id => user_id,
    :partner_id => partner_id
}).
where('entries.date <= :last_day', { 
    :last_day => Date.new(year, month, 1).at_end_of_month
}).
select('min(entries.date) as date, ' + 
       'sum(case when joint = false then amount_calc else 0 end) as sum_private, ' + 
       'sum(case when joint = true and user_id = ' + user_id.to_s + ' then amount_calc else 0 end) as sum_user_joint, ' + 
       'sum(case when joint = true and user_id = ' + partner_id.to_s + ' then amount_calc else 0 end) as sum_partner_joint, ' +              
       'sum(case when compensation = true and user_id = ' + user_id.to_s + ' then amount_calc else 0 end) as sum_user_compensation, ' +
       'sum(case when compensation = true and user_id = ' + partner_id.to_s + ' then amount_calc else 0 end) as sum_partner_compensation '
).
group('EXTRACT(YEAR FROM "date"), EXTRACT(MONTH FROM "date")')

…and works like expected.

Another of my statement runs into troubles now and I edit it here as it seems related to the answer of peufeu. Model/Controller:

   def self.all_entries_month(year, month, user_id, partner_id)
    mydate = Date.new(year, month, 1)

    where(':user_id = entries.user_id OR (:partner_id = entries.user_id AND entries.joint = :true)', {
        :user_id => user_id,
        :partner_id => partner_id,
        :true => true
    }).
    where(':first_day <= entries.date AND entries.date <= :last_day', { 
        :first_day => mydate,
        :last_day => mydate.at_end_of_month
    })
  end

  # group by tag and build sum of groups named group_sum
  def self.group_by_tag
    group('tag').
    select('entries.*, sum(amount_calc) as group_sum')
  end

controller:

@income = Entry.all_entries_month(@year, @month, current_user.id, current_partner.id).income
@cost = Entry.all_entries_month(@year, @month, current_user.id, current_partner.id).cost

# group cost by categories
@group_income = @income.group_by_tag.order('group_sum desc')
@group_cost = @cost.group_by_tag.order('group_sum')

The error is:

ActionView::Template::Error (PGError: ERROR:  column "entries.id" must appear in the GROUP BY clause or be used in an aggregate function
2011-05-03T18:35:20+00:00 app[web.1]: : SELECT entries.*, sum(amount_calc) as group_sum FROM "entries" WHERE (1 = entries.user_id OR (2 = entries.user_id AND entries.joint = 't')) AND ('2011-04-01' <= entries.date AND entries.date <= '2011-04-30') AND (amount_calc <= 0 AND compensation = 'f') GROUP BY tag ORDER BY group_sum):
2011-05-03T18:35:20+00:00 app[web.1]:     6:    </thead>
2011-05-03T18:35:20+00:00 app[web.1]:     7:    <tbody>
2011-05-03T18:35:20+00:00 app[web.1]:     8:        <% if categories %>
2011-05-03T18:35:20+00:00 app[web.1]:     9:            <% categories.each do |category| %>     
2011-05-03T18:35:20+00:00 app[web.1]:     10:               <tr>
2011-05-03T18:35:20+00:00 app[web.1]:     11:                   <td class="align-left"><%= category.tag %></td>
2011-05-03T18:35:20+00:00 app[web.1]:     12:                   <td class="align-right"><%= my_number_to_percentage (category.group_sum.to_f / total_cost) * 100 %></td>

UPDATE 2: I found the solution

  # group by tag and build sum of groups named group_sum
  def self.group_by_tag
    group('tag').
    select('tag, sum(amount_calc) as group_sum')
  end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T21:52:21+00:00Added an answer on May 21, 2026 at 9:52 pm

    Problem is there :

    EXTRACT(YEAR FROM TIMESTAMP date)||EXTRACT(MONTH FROM TIMESTAMP date)
    

    Solution :

    EXTRACT(YEAR FROM "date"), EXTRACT(MONTH FROM "date")
    

    The word “TIMESTAMP” is a bit out of place there 😉 … also :

    Since DATE is a reserved SQL keyword, it is a very bad idea to use it as a column name. Here, I quoted it using ” so postgres doesn”t get confused.

    And you don’t need to waste CPU time building a string concatenating your two EXTRACTs, just remember GROUP BY can use several parameters.

    Then of course the query will fail because you SELECT “date” but your dont’ GROUP BY date. postgres has no way to know which date you want from all the rows which have the same Year-Month. MySQL will return a value at random from the rows, postgres likes correctness so it will throw an error. You could SELECT min(date) for instance, which would be correct.

    I am uncertain about the case when joint = “t”

    That depends on how you want to get your results, nothing wrong there.

    Maybe using several queries would scan a smaller portion of the table (I don’t know about your dataset) or maybe not.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So after pushing my app to heroku, I tried to migrate database to heroku.
Before my app was on Heroku and used mysql gem. When I migrate that
I'm having trouble updating my rails app on heroku. I've gotten my app up
FIXED : SEE BELOW ok. so here's my app. http://libwiki.heroku.com/libraries if you view it
I'm running a MySQL database locally for development, but deploying to Heroku which uses
For example how do you push code to specific heroku application using the heroku_san
I recently forked https://github.com/fortuity/rails3-mongoid-omniauth and tried to get login working for different providers. It
I'm building an application that will require CouchDB's mobile syncing feature. So for each
I'm trying to create a job in order to send a notification to a
I've just started using Ruby on Rails and so far it's working nicely. I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.