首页 > 解决方案 > 带有查询的 Unix shell 脚本不会为带有 where 子句的查询生成数据,但如果没有 where 子句,它正在为 postgres 元数据生成数据

问题描述

我在 shell 脚本中使用 Postgres 查询来执行并获得一些结果并加载到文件中。该查询使用 where 子句,其中日期列用于 postgres 数据库。如果没有 where 子句,它正在生成数据,但在查询中使用 where 子句,它不会生成数据。以下是示例查询以供参考。

psql -h host -p port -U username -w -d databasename -Atc 
"with A as(  select max(col1) as max_inst_id,col2,col3,col4 from 
sch_task_Inst where col5 > timestamp '2018-05-05 02:00:00 - interval 
'1 hours'  group by col1,col2,col3,col4  ) 
select b.col1 as task_nm,A.col1 as run_status,A.col2 as run_log from 
A join sch_task b on A.col1 = b.col1  where A.col2 = 'FAI' ;" >> 
/app/ditechstyle/diserver/app/source/Sch_test.dat

有人可以告诉我查询 where 子句有什么问题吗?

标签: postgresqlshellunixscripting

解决方案


您的问题是您在时间戳之后缺少一个撇号。你应该有timestamp '2018-05-05 02:00:00' - interval '1 hours',但你实际拥有的是timestamp '2018-05-05 02:00:00 - interval '1 hours'。注意后面的撇号02:00:00请参阅此页面上有关 PostgreSQL 日期和时间戳的示例。


推荐阅读