在我们的Postgres数据库中,我们想计算自开始以来每天的预订数量。
现在我们有以下查询:
SELECT
to_char("domain".bookings.created_at,'dy') AS "Name of the day",
COUNT ("domain".bookings."id") AS "Number of bookings"
FROM
"domain".bookings
GROUP BY
to_char("domain".bookings.created_at,'dy'),
"domain".bookings."id"
ORDER BY
"domain".bookings."id"
不幸的是,它返回了以下结果:
tue 1
wed 1
wed 1
sun 1
sat 1
thu 1
wed 1
tue 1
而不是每天的总数。
有没有办法改变公式?
谢谢!
卢卡
答案 0 :(得分:2)
这是造成你问题的群体。您不希望按汇总字段进行分组
SELECT
to_char("domain".bookings.created_at,'dy') AS "Name of the day",
COUNT ("domain".bookings."id") AS "Number of bookings"
FROM
"domain".bookings
GROUP BY
to_char("domain".bookings.created_at,'dy')
ORDER BY
to_char("domain".bookings.created_at,'dy')
答案 1 :(得分:2)
您的分组似乎有误。尝试:
SELECT
to_char("domain".bookings.created_at,'dy') AS "Name of the day",
COUNT ("domain".bookings."id") AS "Number of bookings"
FROM
"domain".bookings
GROUP BY
to_char("domain".bookings.created_at,'dy')
ORDER BY
"Number of bookings"