2014-12-26 150 views
0

我将我的SQL存储过程来Postgres的功能,但它在声明中的一个产生的误差转换为Postgres的功能

错误:在或附近有语法错误“” 第7行:CAST(时间( 0),'00:00' +(h.hour *间隔“1小时...... ^

下面是我的Postgres的功能和SQL存储过程对此我converting.Please告诉我,我得到错误

CREATE OR REPLACE FUNCTION shiftwisedata_sp(INOut shift_id 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, 
 
      CAST(time(0), '00:00' + (h.hour * interval '1Hour')) AS from_time, 
 
      CAST(time(0), '00:00' + ((h.hour + 1) * interval '1Hour')) 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) 
 
    OUTER APPLY (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 = shift_id 
 
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; 
 
\t 
 
    END; 
 
$BODY$ 
 
    LANGUAGE plpgsql VOLATILE

CREATE PROCEDURE Shiftdata @date date, @shiftid int AS 
 
    SELECT @date, s.Shift_Name, 
 
      convert(time(0), dateadd(HOUR, h.hour, '00:00')) AS from_time, 
 
      convert(time(0), dateadd(HOUR, h.hour + 1, '00:00')) 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) 
 
    OUTER APPLY (SELECT SUM(r.Reading_Col) AS Readings 
 
       FROM Reading r 
 
       WHERE r.Timestamp_Col >= dateadd(HOUR, h.hour, convert(datetime, @date)) 
 
        AND r.Timestamp_Col < dateadd(HOUR, h.hour + 1, convert(datetime, @date))) AS r 
 
WHERE s.Shift_ID = @shiftid 
 
    AND (s.to_time > s.from_time    AND 
 
     h.hour >= datepart(HOUR, s.from_time) AND 
 
     h.hour < datepart(HOUR, s.to_time) 
 
    OR 
 
     s.to_time < s.from_time AND 
 
     (h.hour >= datepart(HOUR, s.from_time) OR 
 
      h.hour < datepart(HOUR, s.to_time)) 
 
     ) 
 
ORDER BY s.to_time

回答

0

那不是怎么CAST在PostgreSQL工作。见http://www.postgresql.org/docs/9.2/static/sql-createcast.html

('00:00' + (h.hour * interval '1Hour'))::time 

是最容易阅读(恕我直言)如果你不介意使用非标准::符号。

+0

CAST('00:00' +(h.hour *间隔 '1小时')作为时间戳没有时区)AS from_time这是我如何修改是这样吗? – Parth

+0

作为便携式解决方案,这看起来不错。 (我会让你决定,如果你想要一个时间,时间戳等) –

+0

现在,instaed外部申请我使用左连接latreal是postgres,但它导致错误在哪里s.shift_id = shiftid下面是我是什么写入左连接宽度(SELECT SUM(r.param_value)AS读数 FROM table_1 r WHERE r.timestamp_col> = CAST(userdate作为没有时区的时间戳)+ h.hour * interval'1Hour' AND r.timestamp_col Parth

0

最后, 转换SQL SP向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; \t 
 
    END; 
 
$BODY$ 
 
    LANGUAGE plpgsql VOLATILE