PostgreSQL 9.3:错误:WHERE的参数必须是boolean类型,而不是类型文本

时间:2015-12-18 10:03:22

标签: postgresql postgresql-9.3

我创建了包含五列的下表。

CREATE TEMP TABLE t_Test (
  colname   text,
  coldt     timestamp,
  coltm     timestamp,
  colaz     text,
  colx      integer  
);

插入

INSERT INTO t_Test VALUES 
  ('X','2010-01-01','1900-01-01 01:01:25', 'Green', 1), ('B','2010-01-02','1900-01-01 11:32:15', 'Red', 2)
, ('Z','2010-01-03','1900-01-01 02:01:25', 'Green', 4), ('E','2010-01-04','1900-01-01 11:01:55', 'Red', 5)
, ('G','2010-01-05','1900-01-01 03:05:25', 'Red', 7);

注意:现在我想以我正在使用的上述数据的数据透视格式显示结果 交叉表查询如下所示。

我想只显示那些与日期和时间条件匹配的记录。

CrossTab查询

SELECT * FROM   crosstab(
      'SELECT colname, colaz, colx
       FROM   t_test
       WHERE to_char(coldt,''YYYY-MM-DD'') || '' '' || to_char(coltm,''HH12:MI:SS'')
      >= to_char(to_date(''01-01-2000'',''dd-mm-yyyy''), ''yyyy-mm-dd'') ||  ''11:50:01'''
)  
AS ct ("ColName" text, "Green" int, "Red" int);

执行错误时:

错误详情

ERROR:  argument of WHERE must be type boolean, not type text
LINE 3:        WHERE to_char(coldt,'YYYY-MM-DD') || ' ' || to_char(c...

1 个答案:

答案 0 :(得分:2)

将等式的两边放在括号中,如下所示

SELECT * FROM   crosstab(
      'SELECT colname, colaz, colx
       FROM   t_test
       WHERE (to_char(coldt,''YYYY-MM-DD'') || '' '' || to_char(coltm,''HH12:MI:SS''))
      >= (to_char(to_date(''01-01-2000'',''dd-mm-yyyy''), ''yyyy-mm-dd'') ||  ''11:50:01''')
)  
AS ct ("ColName" text, "Green" int, "Red" int);