gpt4 book ai didi

PostgreSQL 交叉表 : Month rows and Day columns; Error rowid datatype does not match return rowid datatype

转载 作者:行者123 更新时间:2023-11-29 11:44:18 32 4
gpt4 key购买 nike

我正在尝试创建一个交叉表,其中行 = 月,列 = 天(即 1、2、3、4...31)。

    Month |   1  |   2  |  3  |  4  |  5  |  6  |  7  |  8  |  9  |  10  | 11  |  12 ...
------+------+------+-----+-----+-----+-----+-----+-----+-----+-----+------+------
9 | 1000 | 1500 | | | | | 500 | | | | 1500 | 2000
8 | 1000 | | | | | | | | | | |

我的查询如下:

SELECT * FROM crosstab(
$$
SELECT
extract(month from created_at) AS themonth,
extract(day from created_at) AS theday,
COUNT(*)
FROM public.users
WHERE created_at >= Now() - Interval '90 Days' AND created_at < Now() - Interval '1 days'
GROUP BY created_at
ORDER BY 1,2
$$
) AS final_result (
themonth int,
theday int
)

以下收到错误:rowid 数据类型与返回 rowid 数据类型不匹配

这是我第一次使用交叉表。

我觉得这是一个简单的修复,非常感谢任何帮助。谢谢!

最佳答案

有两个问题。 final_result 中的行声明必须与函数返回的元组完全匹配。此外,您应该使用带有两个参数的函数变体 crosstab(text source_sql, text category_sql) .

5 天的示例:

SELECT * FROM crosstab(
$$
SELECT
extract(month from created_at) AS themonth,
extract(day from created_at) AS theday,
COUNT(*)
FROM public.users
WHERE created_at >= Now() - Interval '90 Days' AND created_at < Now() - Interval '1 days' AND alternate_email not like '%@flyhomes.com%'
GROUP BY created_at
ORDER BY 1,2
$$,
$$
SELECT generate_series(1, 5) -- should be (1, 31)
$$
) AS final_result (
themonth float, "1" bigint, "2" bigint, "3" bigint, "4" bigint, "5" bigint -- should be up to "31"
)

Working example in rextester.

关于PostgreSQL 交叉表 : Month rows and Day columns; Error rowid datatype does not match return rowid datatype,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52486821/

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