如何查询月份和工作日?
how to query for month and weekday?
我有一个问题,我正在做这样的事情
query =
from(
u in User,
where: u.user_id == ^user_id,
group_by: [
fragment("date_part(?,?)::int", "month", u.inserted_at),
u.user_id
],
select: %{
month: fragment("date_part(?,?)::int", "month", u.inserted_at),
weekly: filter(count(u.user_id), fragment("date_trunc('week', ?) = date_trunc('week', current_timestamp)", u.inserted_at)),
monthly: count(u.user_id),
}
)
我正在尝试获得一个结果,我想知道每个月有多少用户以及工作日和周末有多少用户?
结果会是这样的
[
%{month: 10, users: 5, weekday: 2, weekend: 3},
%{month: 9, users: 5, weekday: 1, weekend: 4}
]
这之后我不知道如何继续,请给我一些建议
PostgreSQL 方言中的一些 SQL 语句。
SELECT
date_part('year', users.inserted_at) AS year, -- remove this line if you think January 2020 and January 2021 are the same month.
date_part('month', users.inserted_at) AS month,
date_part('isodow', users.inserted_at) >= 6 AS weekend,
COUNT(users.id) AS "count"
FROM
users
GROUP BY
year, -- remove this line if you think January 2020 and January 2021 are the same month.
month,
weekend
ORDER BY
year ASC, -- remove this line if you think January 2020 and January 2021 are the same month.
month ASC,
weekend ASC
你不能只通过SQL查询得到你想要的答案,因为users
和weekend
/weekday
的聚合粒度不同。您必须在 RAM 中进行一些计算。
我有一个问题,我正在做这样的事情
query =
from(
u in User,
where: u.user_id == ^user_id,
group_by: [
fragment("date_part(?,?)::int", "month", u.inserted_at),
u.user_id
],
select: %{
month: fragment("date_part(?,?)::int", "month", u.inserted_at),
weekly: filter(count(u.user_id), fragment("date_trunc('week', ?) = date_trunc('week', current_timestamp)", u.inserted_at)),
monthly: count(u.user_id),
}
)
我正在尝试获得一个结果,我想知道每个月有多少用户以及工作日和周末有多少用户?
结果会是这样的
[
%{month: 10, users: 5, weekday: 2, weekend: 3},
%{month: 9, users: 5, weekday: 1, weekend: 4}
]
这之后我不知道如何继续,请给我一些建议
PostgreSQL 方言中的一些 SQL 语句。
SELECT
date_part('year', users.inserted_at) AS year, -- remove this line if you think January 2020 and January 2021 are the same month.
date_part('month', users.inserted_at) AS month,
date_part('isodow', users.inserted_at) >= 6 AS weekend,
COUNT(users.id) AS "count"
FROM
users
GROUP BY
year, -- remove this line if you think January 2020 and January 2021 are the same month.
month,
weekend
ORDER BY
year ASC, -- remove this line if you think January 2020 and January 2021 are the same month.
month ASC,
weekend ASC
你不能只通过SQL查询得到你想要的答案,因为users
和weekend
/weekday
的聚合粒度不同。您必须在 RAM 中进行一些计算。