【问题标题】:Syntax error at or near "WHERE"“WHERE”处或附近的语法错误
【发布时间】:2015-02-23 15:14:14
【问题描述】:

在创建 postgres 函数时会导致错误

错误:“WHERE”处或附近的语法错误 第 19 行:在哪里 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

【问题讨论】:

  • 不是解决方案,而是:看看 generate_series 内置
  • 1) 确实:generate_series() 可以替换values() 列表,并且2) 日期/时间操作也可以被审查,恕我直言。 3) h.hour 看起来很难看,但这可能是数据建模问题。 4) 也许间隔和重叠也会有所帮助。

标签: postgresql


【解决方案1】:

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

【讨论】:

  • 我在 postgres 和 sql SP 中转换 SQL 存储过程,我已经使用了 OUTER apply 所以什么是等效的 postgres...实际上 ON caluse 用于相同列的连接和我的如果没有相同的列
  • 我以前从未使用过 LATERAL,但从检查查询来看,我认为在有问题的 WHERE 之前添加 ON TRUE 看起来就像您正在寻找的药物。
【解决方案2】:

工作 Postgres 函数

CREATE OR REPLACE FUNCTION shiftwisedata_sp(IN shiftid bigint, INOUT userdate date, OUT shift_name character varying, OUT from_time time without time zone, OUT to_time time without time zone, OUT readings 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 CAST(SUM(r.param_value) as bigint) 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 ON TRUE
WHERE s.shift_id = shiftid
 AND (s.to_time > s.from_time              AND 
       h.hour >= Extract(HOUR from CAST(s.from_time as time)) AND
       h.hour <  Extract(HOUR from CAST(s.to_time as time)) 
    OR
      s.to_time < s.from_time AND
         (h.hour >= Extract(HOUR from CAST(s.from_time as time)) OR
          h.hour < Extract(HOUR from CAST(s.to_time as time))
       ))
      ORDER BY s.to_time;	
  END;
$BODY$
  LANGUAGE plpgsql VOLATILE

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-17
    • 2018-01-26
    • 2014-04-26
    • 1970-01-01
    • 2020-02-23
    • 2012-12-04
    • 2017-02-20
    • 1970-01-01
    相关资源
    最近更新 更多