我什至不确定是否可以做到这一点,但是如果我想创建一个可以计算出一些商务时间事情的函数,并且想要一个代表假期的常量,我可以这样做吗? / p>
CREATE OR REPLACE FUNCTION businesstime(start_time, end_time)
...
DECLARE
HOLIDAYS constant date[] := '{2019-01-01, 2019-07-04, 2019-11-28}'
BEGIN
-- do business time stuff with holidays
END
...
如果是,怎么办?我无法摆脱过去的语法错误,也不确定这是因为我做错了还是不可能的。
谢谢
答案 0 :(得分:1)
示例中的分号缺失。
此代码正在我的计算机上运行:
do $$
declare d date[] default '{2017-01-01, 2018-01-01}';
begin
raise notice '%', d;
end;
$$;
有四种不同的语法,但是结果和性能几乎是相同的(可能会有非常小的性能差异,具体取决于用法):
-- string literal of unknown type with late implicit casting
d := '{2017-01-01, 2018-01-01}';
-- string literal of date[] type
d := _date '{2017-01-01, 2018-01-01}';
几乎没有什么问题-对于日期数组类型,我必须使用备用类型名称_date
。这是一个古老的约定-数组类型的内部名称以前缀_
开头。
-- string literal of unknown type with immediate explicit casting
d := '{2017-01-01, 2018-01-01}'::date[];
d := CAST('{2017-01-01, 2018-01-01}' AS date[]);
-- using array constructor with late implicit casting
d := ARRAY['2017-01-01', '2018-01-01'];
-- using array constructor with casting of array
d := ARRAY['2017-01-01', '2018-01-01']::date[];
-- using array constructor with immediate casting of field
d := ARRAY['2017-01-01'::date, '2018-01-01'];
第一个元素的类型强制数组其他元素的类型
还有更多的方法来写数组常量-但上述方法之间的差异几乎在几乎用例中几乎为零。
答案 1 :(得分:0)
欢迎,终于知道了。只需投放:
HOLIDAYS constant date[] := '{2019-01-01, 2019-07-04, 2019-11-28}'::date[]
尽管仍不确定这是否是最好的方法,所以暂时不要接受我自己的答案。很想知道这是否真的是最好/唯一的方法。
答案 2 :(得分:0)
好的,仅使用SELECT * from Policies PS
INNER JOIN Endorsements E ON E.policy_no = PS.policy_no
INNER JOIN Payment P ON P.client_id = PS.client_no
ORDER BY date ASC
和generate_series
函数在PostgreSQL中执行此操作非常简单,如下所示是PL / PGSQL函数定义和使用示例:
array_agg