Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GroupDate doesn't work with Postgresql databases #251

Closed
Tectract opened this issue May 8, 2021 · 6 comments
Closed

GroupDate doesn't work with Postgresql databases #251

Tectract opened this issue May 8, 2021 · 6 comments

Comments

@Tectract
Copy link

Tectract commented May 8, 2021

Hi there!

Great tool, but I ran into issues trying to run it on my postgresql systems.

Order.group_by_day(:created_at)

ActiveRecord::StatementInvalid: PG::GroupingError: ERROR: column "orders.id" must appear in the GROUP BY clause or be used in an aggregate function

It appears this is a quirk of how the .group statement works in postgresql. Any thoughts on how to get around it?

@Tectract
Copy link
Author

Tectract commented May 9, 2021

OK, I found a working query for orders_by_day for my shopping cart application... it varies from your queries a bit...

your query form:

orders.where(:created_at => start_time.beginning_of_day..Time.zone.now.end_of_day)
.group("date(created_at)")
.select("date(created_at), sum(total_price) as total_price")

my new query form:

orders.where(:created_at => start_time.beginning_of_day..Time.zone.now.end_of_day)
.group("date(created_at)").
.select("date(created_at) as created_at, array_agg(orders.id), sum(total_price) as total_price")

This fixes the problem and allows me to elegantly group/select and sum order totals within date ranges for my graphs, as the gem is supposed to do... but within postgresql

@ankane
Copy link
Owner

ankane commented May 10, 2021

Hey @Tectract, you need to specify an aggregate method like count, sum, average, etc:

Order.group_by_day(:created_at).count

Groupdate's methods behave the same as Active Record's group method - replace group_by_day with group and you'll see the same error:

Order.group(:created_at) # same error

@Tectract
Copy link
Author

.sum wasn't working for me, which is what I needed.

@dustineichler
Copy link

@Tectract you can find a possible resolution here:

#246 (comment)

@Tectract
Copy link
Author

Here was my solution for group_by_week, for reference... I looked at your code, this is sort of what I expected to see, lol.


def orders_chart_series_weekly_totals(orders,start_time)
    if orders == nil
      orders = Order.all
    end
    totals = {}
    orders_by_day = orders.where(:created_at => start_time.beginning_of_day..Time.zone.now.end_of_day)
      .group("date_trunc('week',created_at)")
      .select("date_trunc('week',created_at) as created_at, array_agg(orders.id), sum(orders.order_total) as order_total")
    (start_time.to_date.beginning_of_week..Date.today.end_of_week).step(7).map do |date|
      order = orders_by_day.detect { |order| order.created_at.to_date == date }
      totals["#{date}"] = order && order.order_total.to_f/100 || 0.0
    end
    return totals
  end

@ankane
Copy link
Owner

ankane commented May 13, 2021

Glad you found something that works.

@ankane ankane closed this as completed May 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants