我試圖獲得特定列的總和。 我有一個訂單模式,總場地,存儲總價格。 現在我試圖創建一個將總計所有訂單的總價值的查詢,但不知道我是否做得正確。 這裏是我到目前爲止有:Elixir/Phoenix列的總和
def create(conn, %{"statistic" => %{"date_from" => %{"day" => day_from, "month" => month_from, "year" => year_from}}}) do
date_from = Ecto.DateTime.cast!({{year_from, month_from, day_from}, {0, 0, 0, 0}})
revenue = Repo.all(from p in Order, where: p.inserted_at >= ^date_from, select: sum(p.total))
render(conn, "result.html", revenue: revenue)
end
而只是調用它像html.eex <%= @revenue %>
。 截至目前,它不會返回錯誤,只是在頁面上呈現隨機符號,而不是總收入。
我想我的查詢是錯誤的,但找不到有關如何使其正常工作的良好信息。任何幫助表示感謝,謝謝!
是否使用的是外生的版本? – Gazler
試試'Repo.one'而不是'Repo.all'。 – Dogbert
@Dogbert該死的,不敢相信我犯了這個錯誤,謝謝! – Ilya