这是我的代码:
INSERT INTO salaries (num)
SELECT
CASE random()<0.5 WHEN true THEN min(8+floor(random()*4),10))
WHEN false THEN max(8-floor(random()*4),1)) END AS num
FROM generate_series(1,1) as seq(my_id);
返回此错误:
LINE 3: CASE random() < 0.5 WHEN true THEN (SELECT min(8+floor(rando...
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
你能给我一点提示吗?或者我做错了什么?
发布于 2020-04-20 17:24:48
当前的问题是您需要least()
和greatst()
,而不是min()
和max()
-后者是聚合函数,可以在集合上操作,而不是在值列表上操作。
但是我也认为你的查询没有做你想做的事情。每次调用random()
时都会重新计算它。想必,您希望在整个case
表达式中有一个一致的值,所以如下所示:
insert into salaries (num)
select case when rnd < 0.5
then least(8 + floor(rnd * 4), 10)
else greatest(8 - floor(rnd * 4), 1)
end
from (select my_id, random() rnd from generate_series(1,1) as seq(my_id)) t
目前还不清楚generate_series()
的目的是什么,但我认为这是过于简化的查询的副作用.
https://stackoverflow.com/questions/61328207
复制相似问题