开发者

SQLite/Postgres/Heroku: Problem translating query

开发者 https://www.devze.com 2023-03-02 04:10 出处:网络
Heroku throws an error on my Postgres-Query stating: ActiveRecord::StatementInvalid (PGError: ERROR:syntax error at or

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_cal开发者_StackOverflowc 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


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.


I don't know ruby/heroku, but the expression

joint = "t"

refers to a column t in PostgreSQL because object names are quoted with double quotes. So unless Ruby/Heroku is replacing those double quotes with single quotes that will be an invalid condition.

If t should be string literal (the character t) then you need to use single quotes: joint = 't'

If joint is of type boolean then you should use joint = true in PostgreSQL

0

精彩评论

暂无评论...
验证码 换一张
取 消