在SQL Server上显示非零值

发布于 2025-02-11 17:57:21 字数 997 浏览 0 评论 0原文

请查询如何使所有日期在我的查询中显示在SQL Server上。我试图生成_系列,但正在努力努力将其拟合:以下查询中的代码。

WITH 
    cte_data AS (
        SELECT
            customer_id,
            FORMAT(event_time,'yyyy-MM') as _month,
            'eventtype1' AS source
        FROM Pay.Money
        UNION 
        SELECT
            customer_id,
            FORMAT(event_time,'yyyy-MM') as _month,
            'eventtype2' AS source
        FROM Pay.Send)

SELECT
    _month,
    COUNT(customer_id) AS active_customer
FROM(
    SELECT 
        customer_id,
        _month,
        _no
    FROM(    
        SELECT
            customer_id,
            _month,
            count(customer_id) AS _no
        FROM cte_data
        GROUP BY
            _month,
            customer_id)l
    WHERE _no>2)j
GROUP BY _month

出现的数据显示了我想要的东西,但我想显示数月的0个值,而这些值当前没有数据,

我想要的是

10-2021 5
12-2021 9
02-2022  2

我想要的

10-2021 5
11-2021 0
12-2021 9
01-2022 0
02-2022 2

have a query on how to get all dates to show in my query on SQL server. I have tried to GENERATE_SERIES but was struggling on how to fit it in: code below on query.

WITH 
    cte_data AS (
        SELECT
            customer_id,
            FORMAT(event_time,'yyyy-MM') as _month,
            'eventtype1' AS source
        FROM Pay.Money
        UNION 
        SELECT
            customer_id,
            FORMAT(event_time,'yyyy-MM') as _month,
            'eventtype2' AS source
        FROM Pay.Send)

SELECT
    _month,
    COUNT(customer_id) AS active_customer
FROM(
    SELECT 
        customer_id,
        _month,
        _no
    FROM(    
        SELECT
            customer_id,
            _month,
            count(customer_id) AS _no
        FROM cte_data
        GROUP BY
            _month,
            customer_id)l
    WHERE _no>2)j
GROUP BY _month

My data that comes out shows what I want but I would like to show 0 values for months that have no data next to them

Currently data is

10-2021 5
12-2021 9
02-2022  2

What I would like

10-2021 5
11-2021 0
12-2021 9
01-2022 0
02-2022 2

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文