[英]Ecto Elixir: Sum values in preloaded association
I have a database which has different songs that can have multiple requests and each request can in part have multiple votes. 我有一个数据库,其中有不同的歌曲,可以有多个请求,每个请求可以部分地具有多个投票。
I'm trying to build a query that gets all of the info for a request (song information and votes made by users) but I just want to select the important information I need. 我正在尝试建立一个查询,以获取请求的所有信息(歌曲信息和用户投票),但我只想选择所需的重要信息。
I currently have it set up like this 我目前有这样的设置
def get_requests_in_zone(zone_id, max_played) do
query = from request in Request,
left_join: song in assoc(request, :song),
where: request.zone_id == ^zone_id and request.times_played <= ^max_played,
select: request,
preload: [song: song],
order_by: [asc: request.times_played, desc: request.inserted_at]
Repo.all(query)
end
def calculate_rating(request_id)do
query = from votes in Vote,
where: votes.request_id == ^request_id,
select: sum(votes.rating)
Repo.one(query)
end
def get_requests_in_zone_with_votes(zone_id,max_played) do
Enum.map(get_requests_in_zone(zone_id,max_played),
fn(x) ->
%{title: x.song.title, url: x.song.url, thumbnail: x.song.thumbnail, rating: calculate_rating(x.id)} end)
end
But that makes multiple queries to calculate the rating for each request. 但这会使多个查询计算出每个请求的等级。 I tried building a single query, it was something akin to: 我试图建立一个查询,这类似于:
def get_requests_in_zone(zone_id, max_played) do
query = from song in Song,
left_join: request in assoc(song, :requests),
join: votes in assoc(request, :votes),
where: request.zone_id == ^zone_id and request.times_played <= ^max_played,
select: %{title: song.title, url: song.url, thumbnail: song.thumbnail, rating: sum(votes.rating)},
preload: [requests: {request, votes: votes}],
order_by: [asc: request.times_played, desc: request.inserted_at]
Repo.all(query)
end
But it didn't work... Giving out this error message 但这没用...给出此错误消息
** (Ecto.QueryError) field .Song.request
in preload is not an association in query: **(Ecto.QueryError)字段.Song.request
中的.Song.request
不是查询中的关联:
So I decided to add the complete song in the select by changing the select line to 因此,我决定通过将选择行更改为
select: %{song: song, rating: sum(votes.rating)},
And it just said ERROR 42803 (grouping_error): column "s0.id" must appear in the GROUP BY clause or be used in an aggregate function 它只是说错误42803(grouping_error):列“ s0.id”必须出现在GROUP BY子句中或在聚合函数中使用
How could I build a query that retrieves the requests and returns a struct similar to the one used in get_requests_in_zone_with_votes? 如何建立查询以检索请求并返回类似于get_requests_in_zone_with_votes中使用的结构的查询?
Thanks for you time. 谢谢您的时间。
Calculate the sum in a subquery, and join to it from the main query: 计算子查询中的总和,然后从主查询中加入:
q1 = from votes in Vote,
group_by: votes.request_id,
select: %{request_id: votes.request_id, total_rating: sum(votes.rating)}
q2 = from request in Request,
left_join: song in assoc(request, :song),
left_join: rating in subquery(q2), on: request.id == rating.request_id,
where: request.zone_id == ^zone_id and request.times_played <= ^max_played,
select: %{title: song.title, url: song.url, thumbnail: song.thumbnail, rating: rating.total_rating},
order_by: [asc: request.times_played, desc: request.inserted_at]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.