gpt4 book ai didi

MYSQL按 "gaps"填充分组

转载 作者:行者123 更新时间:2023-11-29 02:23:19 26 4
gpt4 key购买 nike

我试图通过使用辅助表来填补使用组后的空白,你能帮忙吗?

用于处理无订单天数的辅助表

date        quantity
2014-01-01 0
2014-01-02 0
2014-01-03 0
2014-01-04 0
2014-01-05 0
2014-01-06 0
2014-01-07 0

按“订单”表的结果分组

date        quantity
2014-01-01 7
2014-01-02 1
2014-01-04 2
2014-01-05 3

将“订单”表与“辅助表”连接起来的期望结果

date        quantity
2014-01-01 7
2014-01-02 1
2014-01-03 0
2014-01-04 2
2014-01-05 3
2014-01-06 0
2014-01-07 0

最佳答案

在不知道如何创建group by result 表的情况下,您在outer join 中查找的内容可能是coalesce。像这样:

select distinct a.date, coalesce(b.quantity,0) quantity
from aux a
left join yourgroupbyresults b on a.date = b.date

请注意,您可能需要也可能不需要 distinct -- 取决于您的数据。


编辑,考虑到您的意见,这应该有效:

select a.date, count(b.date_sent)
from aux a
left join orders b on a.date = date_format(b.date_sent, '%Y-%m-%d')
group by a.date

关于MYSQL按 "gaps"填充分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27695996/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com