我的查询,在原始SQL中,是:
DB.fetch(' SELECT
p.project_name,
p.project_id,
COUNT(i.expense_project_id) AS item_count,
SUM(COALESCE(amount,0)) AS item_amount
FROM expense_projects p
LEFT JOIN expense_items i ON p.project_id = i.expense_project_id
WHERE p.project_company_id = ?passed_param
GROUP BY p.project_id, p.project_name').to_a.to_json
如何使用续集ORM编写此查询?
我已经找到了一个如下解决方案。我以前做过多次努力,当我发布这个问题时,我应该把我失败的努力之一包括在内,道歉!在这里回答:
DB[:expense_projects___p].where(:project_company_id=>user_company_id).
left_join(:expense_items___i, :expense_project_id=>:project_id).
select_group(:p__project_name, :p__project_id).
select_more{count(:i__item_id)}.
select_more{sum(:i__amount)}.to_a.to_json
答案 0 :(得分:0)
DB[:expense_projects___p].where(:project_company_id=>user_company_id).
left_join(:expense_items___i, :expense_project_id=>:project_id).
select_group(:p__project_name, :p__project_id).
select_more{count(:i__item_id)}.
select_more{sum(:i__amount)}.to_a.to_json