这是我想要做的:
thirty_days_ago_completed_visits =
from(v in Visit,
select: %{a: count("*")},
where: v.papa_id == ^papa.id,
where:
v.scheduled_for >= ^thirty_days_ago and
v.state in ["completed", "reviewed"]
)
sixty_days_ago_completed_visits =
from(v in Visit,
select: %{b: count("*")},
where: v.papa_id == ^papa.id,
where:
v.scheduled_for >= ^sixty_days_ago and
v.state in ["completed", "reviewed"]
)
future_scheduled_visits =
from(v in Visit,
select: %{c: count("*")},
where: v.papa_id == ^papa.id,
where: v.recurring == false,
where: v.scheduled_for >= ^Timex.now()
)
future_recurring_visits =
from(v in Visit,
select: %{d: count("*")},
where: v.papa_id == ^papa.id,
where: v.recurring == true,
where: v.scheduled_for >= ^Timex.now()
)
from(
a in subquery(thirty_days_ago_completed_visits),
union_all: ^sixty_days_ago_completed_visits,
union_all: ^future_scheduled_visits,
union_all: ^future_recurring_visits
)
|> Repo.all()
|> IO.inspect()
我收到此错误:
**(Ecto.QueryError)子查询/ CTE必须选择源(t),字段(t.field)或地图,在查询中得到:count("*")
:
from v0 in PapaPal.Visit,
where: v0.papa_id == ^"419b3f7f-b74a-42fa-a377-2b7f54886c06",
where: v0.scheduled_for >= ^#DateTime<2020-04-18 19:31:36.774112Z> and v0.state in ["completed", "reviewed"],
select: count("*")
我想要做的最后是四个整数,每个查询一个整数-通过对数据库使用单个SQL查询。
任何提示?
Ecto.Query.API.count/1
的文档以及错误消息本身,明确指出在将Ecto.Query.API.count/1
传递给它时,您必须选择源,字段或地图。下面将工作。
"*"