gpt4 book ai didi

sql - 在没有数据时也提取日期值

转载 作者:行者123 更新时间:2023-11-29 14:37:49 25 4
gpt4 key购买 nike

我有这张 table

records
- id
- date
- typology

我想提取类型学 = 999 的记录的每日计数。我知道每天至少有 1 条记录,但大多数时候我没有类型学 999 的记录。

我试过类似的东西

select count(*) from records where typology = 999 and date > '2017-01-01' group by date order by date asc;

希望得到类似的东西

285 | 2017-01-06 |
307 | 2017-01-07 |
0 | 2017-01-08 |
316 | 2017-01-09 |

但我没有。我明白了

285 | 2017-01-06 |
307 | 2017-01-07 |
316 | 2017-01-09 |

没错。由于在 1 月 8 日没有类型为 999 的记录,所以我没有得到该行。但我愿意。我不必“发明”2017-01-08 日期,肯定有该日期的记录(但类型不同)。

我尝试了一些合并和子查询,但没有得到太多,主要是因为我有两个字段,而合并在我看来只与一个字段一起工作。

我也试过一个案例

SELECT CASE c WHEN NULL THEN 0 ELSE c END
FROM (
select count(*) as c from records where typology = 892 and date > '2017-01-01' group by date order by date asc
) as s;

也没用。我很确定这是一个非常困惑的查询。

你对我有什么建议吗?

谢谢马可

最佳答案

试试这个:

select t.date,
count(r.date)
from records r
right join
( select date from records where date > '2017-01-01' group by date
) t
on r.date = t.date
and r.typology = 999
group by t.date
order by t.date asc;

关于sql - 在没有数据时也提取日期值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41681326/

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