我需要帮助编写子查询

发布于 2025-01-11 10:29:56 字数 1493 浏览 0 评论 0原文

我有一个像这样的查询来创建日期系列:

Select month
    From
    (select to_char(created_date, 'Mon') as Month,
           created_date::date as start_day,
           (created_date::date + interval '1 month - 1 day ')::date as end_day
    from generate_series(date '2021-01-26', 
                         date '2022-04-26', interval '1 month') as g(created_date)) AS "thang"

表格如下所示:

月份
Jan
Feb
Mar
Apr
May
Jun
Jul
Aug
Sep
Oct

现在我想计算 KYC< 中的 status /代码>表。

所以我尝试这个:

Select
    (Select month
    From
    (select to_char(created_date, 'Mon') as Month,
           created_date::date as start_day,
           (created_date::date + interval '1 month - 1 day ')::date as end_day
    from generate_series(date '2021-01-26', 
                         date '2022-04-26', interval '1 month') as g(created_date)) AS "thang"),
    count(*) filter (where status = 4) as "KYC_Success"
From kyc
group by 1

我希望结果是这样的:

Month |  KYC_Success
Jan   |      234
Feb   |      435
Mar   |      546
Apr   |      157

但是它说

错误:用作表达式的子查询返回多行

我应该在此查询中更改什么?

I have a query like this to create date series:

Select month
    From
    (select to_char(created_date, 'Mon') as Month,
           created_date::date as start_day,
           (created_date::date + interval '1 month - 1 day ')::date as end_day
    from generate_series(date '2021-01-26', 
                         date '2022-04-26', interval '1 month') as g(created_date)) AS "thang"

And the table looks like this:

month
Jan
Feb
Mar
Apr
May
Jun
Jul
Aug
Sep
Oct

Now I want to count the status from the KYC table.

So I try this:

Select
    (Select month
    From
    (select to_char(created_date, 'Mon') as Month,
           created_date::date as start_day,
           (created_date::date + interval '1 month - 1 day ')::date as end_day
    from generate_series(date '2021-01-26', 
                         date '2022-04-26', interval '1 month') as g(created_date)) AS "thang"),
    count(*) filter (where status = 4) as "KYC_Success"
From kyc
group by 1

I hope the result will be like this:

Month |  KYC_Success
Jan   |      234
Feb   |      435
Mar   |      546
Apr   |      157

But it said

error: more than one row returned by a subquery used as an expression

What should I change in this query?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鹿港小镇 2025-01-18 10:29:56

让我们假设表 KYC 有一个名为 created_date 的时间戳列和状态列,并且您想要计算每月的成功状态 - 即使为零一个月内的成功项目。

SELECT thang.month
    , count(CASE WHEN kyc.STATUS = 'success' THEN 1 END) AS successes
FROM (
    SELECT to_char(created_date, 'Mon') AS Month
        , created_date::DATE AS start_date
        , (created_date::DATE + interval '1 month - 1 day ')::DATE AS end_date
    FROM generate_series(DATE '2021-01-26', DATE '2022-04-26', interval '1 month') AS g(created_date)
    ) AS "thang"
LEFT JOIN kyc ON kyc.created_date>= thang.start_date
    AND kyc.created_date < thang.end_date 
GROUP BY thang.month;

Let us assume that the table KYC has a timestamp column called created_date and the status column, and, that you want to count the success status per month - even if there was zero success items in a month.

SELECT thang.month
    , count(CASE WHEN kyc.STATUS = 'success' THEN 1 END) AS successes
FROM (
    SELECT to_char(created_date, 'Mon') AS Month
        , created_date::DATE AS start_date
        , (created_date::DATE + interval '1 month - 1 day ')::DATE AS end_date
    FROM generate_series(DATE '2021-01-26', DATE '2022-04-26', interval '1 month') AS g(created_date)
    ) AS "thang"
LEFT JOIN kyc ON kyc.created_date>= thang.start_date
    AND kyc.created_date < thang.end_date 
GROUP BY thang.month;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文