使用date_trunc在PostgreSQL中创建函数的问题

时间:2014-02-07 11:49:35

标签: postgresql

以下是我的代码示例

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

1 个答案:

答案 0 :(得分:1)

替换行:

 '             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 syntax documentation