簡體   English   中英

使用date_trunc在PostgreSQL中創建函數的問題

[英]Issue in creating a function in PostgreSQL using date_trunc

這是我的代碼示例

v_sql_main:= ' SELECT min_createdate, max_createdate, createdate, customerid::integer, deviceid::integer, null::bigint as sourceip, null::bigint as sourceip_int, service,  total, end_recordid::bigint '||
 ' FROM ( '||
 ' SELECT min(date_trunc( '||quote_literal('HOUR')||' , firstoccurrence)) as  min_createdate, '||
 '        max(date_trunc( '||quote_literal('HOUR')||' , firstoccurrence)) as  max_createdate, '||
 '        date_trunc( '||quote_literal('DAY')||' , firstoccurrence) as  createdate,  '||
 '        customerid::integer,  '||
 '        deviceid::integer, '||
 '        service, '||
 case  when v_days < 4 then 
 '        count(1)  as   total '
 else
 '        sum(summcount)  as   total '
 end ||', max(recordid) as end_recordid'
 ' FROM   '|| v_tablename||
 ' LEFT OUTER JOIN '|| v_child_tablename||
 ' ON ' ||v_tablename||'.SERVICE_ID = '|| v_child_tablename||'.SERVICE_ID '||
 '        WHERE  '||
 '             customerid = v_customerid   AND '||
 '             deviceid   = v_deviceid     AND '||
 '             date_trunc( '||quote_literal('DAY')||' , firstoccurrence) = date_trunc( '||quote_literal('DAY')||' ,now()- interval '1 day') '||
 ' group by date_trunc( '||quote_literal('DAY')||' , firstoccurrence),  customerid, deviceid, service ) as a order by total desc limit 10;;';

當我嘗試執行此操作時,出現以下錯誤

ERROR:  syntax error at or near "1"
LINE 144: ...unc( '||quote_literal('DAY')||' ,now()- interval '1 day') '|...

我需要得到的是:date-1

在此先感謝SHABEER

替換行:

 '             date_trunc( '||quote_literal('DAY')||' , firstoccurrence) = date_trunc( '||quote_literal('DAY')||' ,now()- interval '1 day') '||

通過:

'             date_trunc( '||quote_literal('DAY')||' , firstoccurrence) = date_trunc( '||quote_literal('DAY')||' ,now()- interval '' 1 day'') '||

請看一下Interval語法文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM