Ruby Sequel 子查询发生在主查询之前
Ruby Sequel Subquery Happening Before Main Query
有人可以帮我解决这个问题吗?
我需要将以下 sql 代码翻译成 sequel:
SELECT DISTINCT sales_leads.employee_person_id,
(SELECT full_name from persons
WHERE sales_leads.employee_person_id=persons.id),
(SELECT COUNT (sales_leads.id) from sales_leads
WHERE sales_leads.employee_person_id=employees.person_id
AND sales_leads.status='open') AS open_leads_count,
(SELECT COUNT (sales_leads.id) from sales_leads
WHERE sales_leads.employee_person_id=employees.person_id
AND sales_leads.status='lost' OR sales_leads.status='won') AS closed_leads_count
FROM sales_leads
JOIN employees ON sales_leads.employee_person_id=employees.person_id
ORDER BY sales_leads.employee_person_id;
这是我目前拥有的:
DB[:sales_leads]
.inner_join(:persons, id: :employee_person_id)
.select(:full_name)
.select_append(
DB[:sales_leads]
.select(:id)
.where(Sequel[:sales_leads][:employee_person_id]=>Sequel[:persons][:id])
.count
).all
但是我得到一个错误:
PG::UndefinedTable: ERROR: missing FROM-clause entry for table
"persons" LINE 1: ...eads" WHERE ("sales_leads"."employee_person_id" =
"persons"....
^: SELECT count(*) AS "count" FROM "sales_leads" WHERE
("sales_leads"."employee_person_id" = "persons"."id") LIMIT 1
Sequel::DatabaseError: PG::UndefinedTable: ERROR: missing FROM-clause
entry for table "persons" LINE 1: ...eads" WHERE
("sales_leads"."employee_person_id" = "persons"....
^ from /home/john/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/sequel-5.5.0/lib/sequel/adapters/postgres.rb:145:in
`async_exec'
您在传递给 select_append 调用的数据集上调用 Dataset#count
,并且 Dataset#count
运行查询。你可能想要 .select{count(sales_leads[:id])}
而不是 .count
.
有人可以帮我解决这个问题吗?
我需要将以下 sql 代码翻译成 sequel:
SELECT DISTINCT sales_leads.employee_person_id,
(SELECT full_name from persons
WHERE sales_leads.employee_person_id=persons.id),
(SELECT COUNT (sales_leads.id) from sales_leads
WHERE sales_leads.employee_person_id=employees.person_id
AND sales_leads.status='open') AS open_leads_count,
(SELECT COUNT (sales_leads.id) from sales_leads
WHERE sales_leads.employee_person_id=employees.person_id
AND sales_leads.status='lost' OR sales_leads.status='won') AS closed_leads_count
FROM sales_leads
JOIN employees ON sales_leads.employee_person_id=employees.person_id
ORDER BY sales_leads.employee_person_id;
这是我目前拥有的:
DB[:sales_leads]
.inner_join(:persons, id: :employee_person_id)
.select(:full_name)
.select_append(
DB[:sales_leads]
.select(:id)
.where(Sequel[:sales_leads][:employee_person_id]=>Sequel[:persons][:id])
.count
).all
但是我得到一个错误:
PG::UndefinedTable: ERROR: missing FROM-clause entry for table "persons" LINE 1: ...eads" WHERE ("sales_leads"."employee_person_id" = "persons".... ^: SELECT count(*) AS "count" FROM "sales_leads" WHERE ("sales_leads"."employee_person_id" = "persons"."id") LIMIT 1 Sequel::DatabaseError: PG::UndefinedTable: ERROR: missing FROM-clause entry for table "persons" LINE 1: ...eads" WHERE ("sales_leads"."employee_person_id" = "persons".... ^ from /home/john/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/sequel-5.5.0/lib/sequel/adapters/postgres.rb:145:in `async_exec'
您在传递给 select_append 调用的数据集上调用 Dataset#count
,并且 Dataset#count
运行查询。你可能想要 .select{count(sales_leads[:id])}
而不是 .count
.