简体   繁体   English

如何在 postgreSQL 中创建 generate_series function?

[英]How to create generate_series function in postgreSQL?

I saw at many places on the internet that you could use generate_series function (in postgreSQL ) like shown below:我在互联网上的许多地方看到您可以使用 generate_series function (在postgreSQL中),如下所示:

select k, percentile_disc(k) within group (order by things.value)
from things, generate_series(0.01, 1, 0.01) as k
group by k

And it works like:-它的工作原理如下: -

generate_series generates temporary table with values between a starting and ending value, with an optional step. generate_series 生成临时表,其值介于起始值和结束值之间,步骤可选。 In this example, generate_series(0.01, 1, 0.01) returns 0.01, 0.02, etc..在此示例中, generate_series(0.01, 1, 0.01) 返回 0.01, 0.02 等。

But in my case it shows error saying it does not exist.但在我的情况下,它显示错误,说它不存在。

QL Error [42883]: ERROR: function generate_series(numeric, integer, numeric) does not exist Hint: No function matches the given name and argument types. QL 错误 [42883]: 错误: function generate_series(numeric, integer, numeric) 不存在You may need to add explicit type casts.您可能需要添加显式类型转换。

Could anyone please help me with the code on how to create this function explicitly.谁能帮我提供有关如何明确创建此 function 的代码。

It will be of great help!会有很大帮助的!

Your code should work.您的代码应该可以工作。 But you can just use integers and divide:但是您可以只使用整数并除以:

select k / 100.0, percentile_disc(k / 100.0) within group (order by things.value)
from things cross join
     generate_series(1, 100, 1) as k
group by k

I want to point out that even this code works:我想指出,即使这段代码也有效:

select generate_series(0.01::numeric, 1::int, 0.01::numeric)

And tested going back to Postgres 9.5 on db<>fiddle.并在 db<>fiddle 上测试回到 Postgres 9.5。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM