Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax error at or near "WHERE"

Tags:

postgresql

While creating postgres function it is resulting an error as

ERROR: syntax error at or near "WHERE" LINE 19: WHERE s.shift_id = shiftid ^ ********** Error **********

ERROR: syntax error at or near "WHERE" SQL state: 42601 Character: 108

Please Help..

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
like image 493
PDTech Avatar asked Dec 26 '14 08:12

PDTech


People also ask

What Is syntax error at or near?

Syntax Error, an error that can occur due to a slight carelessness of the software, occurs during coding. The error that usually occurs when writing the source code can confuse the site. Such errors, which can also occur in computer programs, can confuse the computer.

Why am I getting a syntax error in SQL?

Misspellings are the most common cause for error in SQL. Unfortunately, SQL will not autocorrect mistyped keywords, tables, columns, or values. Check keyword spelling by referring to the documentation for the type of SQL you are using.

What is SQL error?

SQLError() returns the diagnostic information associated with the most recently called DB2® for i CLI function for a particular statement, connection, or environment handle. The information consists of a standardized SQLSTATE, an error code, and a text message.


1 Answers

looks like the syntax error is LEFT JOIN needs an ON clause before the WHERE

like image 100
Jasen Avatar answered Sep 29 '22 01:09

Jasen