Using SQL function as group by field in datamapper (ruby)

I'm trying to use a slightly more interesting GROUP BY reason than what the DataMapper out of the box seems to support.

The query I want to create looks something like this:

SELECT SUM(downloads), SUM(uploads) FROM daily_stats GROUP BY YEARWEEK(date)

I would just like to say DataMapper something like that:

DailyStat.aggregate(:downloads.sum, :uploads.sum, :fields => [ :date.yearweek ])

Where to begin? Is it easily achievable?

+3
source share
1 answer

I would really like the same thing, unfortunately, dm-units are not written as possible.

My recommendation is to flush SQL as you get the sums from your table.

DataMapper.repository.adapter.select('SELECT SUM(downloads), SUM(uploads) FROM daily_stats GROUP BY YEARWEEK(date)')

This should give you an array of structures with summarized downloads and downloads.

+3
source

Source: https://habr.com/ru/post/1760148/


All Articles