gpt4 book ai didi

postgresql - "WHERE"处或附近的语法错误

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

创建 postgres 函数时出现错误

错误:“WHERE”处或附近的语法错误第 19 行:WHERE s.shift_id = shiftid ^********** 错误 **********

错误:“WHERE”处或附近的语法错误SQL 状态:42601字符数:108

请帮忙..

CREATE OR REPLACE FUNCTION shiftwisedata_sp(INOut shiftid bigint,InOut userdate date,OUT shift_name character varying (50),OUT from_time character varying(50),OUT to_time character varying(50),OUT cal bigint)
RETURNS SETOF record AS
$BODY$
BEGIN
return query
SELECT userdate, s.shift_name,
('00:00' + (h.hour * interval '1Hour'):: time) AS from_time,
('00:00' + ((h.hour + 1) * interval '1Hour'):: time) AS to_time,
COALESCE(r.Readings, 0) AS readings
FROM shift_wise s
CROSS JOIN (VALUES(0), (1), (2), (3), (4), (5), (6), (7), (8), (9),
(10), (11), (12), (13), (14), (15), (16), (17), (18), (19),
(20), (21), (22), (23)) AS h(hour)
LEFT JOIN LATERAL (SELECT SUM(r.param_value) AS Readings
FROM table_1 r
WHERE r.timestamp_col >= CAST(userdate as timestamp without time zone ) + h.hour * interval '1Hour'
AND r.timestamp_col < CAST(userdate as timestamp without time zone ) + h.hour + 1 * interval '1Hour'
) AS r
WHERE s.shift_id = shiftid
AND (s.to_time > s.from_time AND
h.hour >= date_part(HOUR, s.from_time) AND
h.hour < date_part(HOUR, s.to_time)
OR
s.to_time < s.from_time AND
(h.hour >= date_part(HOUR, s.from_time) OR
h.hour < date_part(HOUR, s.to_time))
)
ORDER BY s.to_time;

END;
$BODY$
LANGUAGE plpgsql VOLATILE

最佳答案

看起来语法错误是 LEFT JOIN 在 WHERE 之前需要一个 ON 子句

关于postgresql - "WHERE"处或附近的语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27654911/

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