如何在没有原始 SQL 的情况下使用 Sequel 编写 PostgreSQL 查询?

How can I write a PostgreSQL query using Sequel without raw SQL?

我的原始查询 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

如何使用 Sequel 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
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